Unable to fetch JSON response in Java Script after http client call

I want to fetch the JSON response from backend in Java script after doing the http client call

I am doing this in my code to fetch the response

if (exchange.isSuccess()) {
var res = exchange.getResponse().content;
print(JSON.stringify(res));
context.setVariable("finalResponse",res.asJSON);
}
else {
var res = exchange.getError();
context.setVariable("finalResponse", res.content);
}

 

but I am getting  this in my final response

org.mozilla.javascript.Undefined@0
Solved Solved
0 13 1,141
1 ACCEPTED SOLUTION

@himanshuvijay - I just updated your code to the following and it works. I can see the response assigned to finalResponse

var headers = {'apikey' : 'rrruYxTLgVwWbvCOv56mxj0UxGcAAOab','Content-Type' : 'application/json'};
var jsonRequest = JSON.stringify(request.content.asJSON);

var myRequest = new Request("https://reqres.in/api/users","POST", headers, jsonRequest);
var exchange = httpClient.send(myRequest);
exchange.waitForComplete();

var res;

if (exchange.isSuccess()) {
    res = exchange.getResponse().content.asJSON;
    print("Success: "+JSON.stringify(res));
}
else {
    res = exchange.getError().content;
    print("Error: "+JSON.stringify(res));
}
context.setVariable("finalResponse", res);

NOTE: Please change your API Key as you have posted that in the community. Could be a security issue

View solution in original post

13 REPLIES 13

@himanshuvijay - Take a look at this sample

It has an example of using httpClient to make an API call and then use the response object. 

Side note: Please take a look at Service Callout policy as thats the recommended policy to use when you want to make sidecar calls

After doing the required changes from the sample I am getting the following Java script error.

Execution of JavaScript-3 failed with error: Javascript runtime error: "TypeError: Cannot set property "content" of undefined to "". (JavaScript-3.js:5)"

@himanshuvijay - Can you share the entire JavaScript policy? With that I probably might be able to help you

var headers = {'apikey' : 'rrruYxTLgVwWbvCOv56mxj0UxGcAAOab','Content-Type' : 'application/json'};
var jsonRequest = JSON.parse(context.getVariable("request.content"));

response.content = '';
response.headers['Content-Type'] = 'application/json';


var myRequest = new Request("https://reqres.in/api/users","POST",headers,JSON.stringify(jsonRequest));


var exchange = httpClient.send(myRequest);

exchange.waitForComplete();


if (exchange.isSuccess()) {
var res = exchange.getResponse().content.asJSON;
print(res);
context.setVariable("finalResponse",res);
}
else {
var res = exchange.getError().content;
context.setVariable("finalResponse", res);
}

Basically I have to store the response from http client backend in final response variable.

The response from the backend is json with content type application/json

 

@himanshuvijay - I just updated your code to the following and it works. I can see the response assigned to finalResponse

var headers = {'apikey' : 'rrruYxTLgVwWbvCOv56mxj0UxGcAAOab','Content-Type' : 'application/json'};
var jsonRequest = JSON.stringify(request.content.asJSON);

var myRequest = new Request("https://reqres.in/api/users","POST", headers, jsonRequest);
var exchange = httpClient.send(myRequest);
exchange.waitForComplete();

var res;

if (exchange.isSuccess()) {
    res = exchange.getResponse().content.asJSON;
    print("Success: "+JSON.stringify(res));
}
else {
    res = exchange.getError().content;
    print("Error: "+JSON.stringify(res));
}
context.setVariable("finalResponse", res);

NOTE: Please change your API Key as you have posted that in the community. Could be a security issue

Even after doing the suggested changes I am getting this as final response

org.mozilla.javascript.Undefined@0

Also it is printing this as output

Error :  Undefined

I recommend using the callback option for httpClient. It looks like this: 

    function onComplete(response, error) {
      if (response) {
        context.setVariable('example.status', response.status);
        context.setVariable('example.content', response.content);
      }
      else {
        context.setVariable('example.error', 'Whoops: ' + error);
      }
    }

    var headers = {
      'apikey' : 'rrruYxTLgVwWbvCOv56mxj0UxGcAAOab',
      'Content-Type' : 'application/json'
    };
    var payload = '{}'; // request.content;
    var myRequest = new Request("https://reqres.in/api/users",
                                "POST",
                                headers,
                                payload);

    httpClient.send(myRequest, onComplete);

 

I tried this and it works for me. 

Attached please find the working API proxy. 

 

here is the trace session for the apiproxy. 

I am getting the following error after doing this . The url that I have to call is different. My http client call is to a api proxy and I have to fetch the json response from it.

I am able to call the proxy but not able to fetch the json response from it

Execution of JavaScript-3 failed with error: Javascript runtime exceeded limit of 200ms

search here on this site and you can find a solution for that time exceeded problem. 

@ssvaidyanathan 

I also have a requirement to do the http client call multiple times(it is not fixed) with different request payload and get the final response for each and do some validation

How can I achieve this  ?

 

How can I achieve this

That's a different question. That's been asked here an answered recently. Search here and you'll find the answer.