简体   繁体   中英

Apigee : Javascript callout policy return error response. How can I resolve this error?

this is my js code in js callout policy

var payload = JSON.parse(request.content);
var headers = {'Content-Type' : 'application/json'};
var url = 'https://jsonplaceholder.typicode.com/posts';
var operation = "POST";
var req = new Request(url, operation, headers, payload);
var calloutResponse1 = httpClient.send(req);
context.session['calloutResponse1'] = calloutResponse1;

I use this curl for call proxy

curl -k https://xxxxxx/poc3 -d '{"name": "Apple MacBook Pro 16", "data": "s"}'

this is response error from js callout policy

{"fault":{"faultstring":"Execution of callapi1 failed on line callapi1_js#5 with error: Unexpected Type. Expected "String" got "NativeObject"","detail":{"errorcode":"steps.javascript.ScriptExecutionFailedLineNumber"}}}

What is problem? How can I resolve this error?

Looks like you are passing payload as a JSON object to Request() . It should be of type String .

var payload = request.content; // Do not parse the JSON

This error message suggests that there is a problem with the JavaScript code you are using in your Apigee Edge JavaScript callout policy. Specifically, it looks like there is an issue with the data being passed as the payload in the call to the external service.

It seems that you are passing the payload as JavaScript Object directly, but the payload content needs to be passed as a String, not an Object. Therefore, You can pass the payload as a string and parse the JSON string into a JavaScript object later.

Here's an example of how you could modify your code to properly pass the payload as a string:

var payload = request.content;
var headers = {'Content-Type' : 'application/json'};
var url = 'https://jsonplaceholder.typicode.com/posts';
var operation = "POST";
var req = new Request(url, operation, headers, payload);
var calloutResponse1 = httpClient.send(req);
var data = JSON.parse(calloutResponse1.content);
context.session['calloutResponse1'] = data;

Also please make sure that your request content are valid JSON.

Additionally, please check external service is running and are available. and check the credentials,headers or any other configurations you need to configure if required to call this external service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM