HTTPClient in Javascript not working? I use HTTPClient in Javascript to make a service callout, but i didn't get a response

Not applicable

Please find the code snippet that i used for making service callout in javascript:

var headers = {'Accept' : 'application/json'};

var request = new Request("targetURL", "GET", headers, "");

var exchange = httpClient.send(request), response, status;

exchange.waitForComplete();

print("exchange.getResponse.status:" + exchange.getResponse().status);

Response:

{ "fault": { "faultstring": "Execution of responseMapping failed with error: Javascript runtime error: \"TypeError: Cannot read property \"status\" from undefined (Test_js#7). at line 7 \"", "detail": { "errorcode": "steps.javascript.ScriptExecutionFailed" } } }

Request is received in target endpoint and it returns a success response. But response from target is not readable in javascript callout. Have anyone ever faced this issue. Kindly help.

2 6 7,725
6 REPLIES 6

hi, I had faced similar issue:

I was trying to run sample code for javascript mashup and it works fine but on changing the endpoint to my apigee proxy UL it was giving error. On debug I noticed that, when you get response from apigee proxy endpoint the it contains the actual response body in that object only. i.e 'exchange' object.

So if you try to print exchange object it will give you response content:

JSON.stringify(exchange);

and so getResponse or status method does not working.

I want the response content only so my issue was resolved but would like to a know about this behavior. Anyone has idea?

@Sonali Even that doesn't work for me. when i try printing my exchange Object is empty { }.

adas
Participant V

@gokulakrishnan.palanisamy This is how I would usually do it:

try
{
    var javascriptCallout = httpClient.get('http://httpbin.org/get');
    context.session["javascriptCallout"] = javascriptCallout;
    response.content = javascriptCallout.getResponse().content;
    response.code = javascriptCallout.getResponse().code;
    context.setVariable("response.header.x-api-status", javascriptCallout.getResponse().status);
} catch (err) {
    // Handle any error that may have happened previously by generating a response
    response.content.asJSON.error = err;
} 

This will return you a response like:


HTTP/1.1 200 OK
Accept: */*
Date: Fri, 29 Apr 2016 16:39:54 GMT
Server: Apigee Router
User-Agent: curl/7.43.0
x-api-status: 200
X-Forwarded-For: 50.204.222.36
X-Forwarded-Port: 80
X-Forwarded-Proto: http
Content-Length: 130
Connection: keep-alive

{
  "args": {},
  "headers": {
    "Host": "httpbin.org"
  },
  "origin": "xx.xxx.xxx.xxx",
  "url": "http://httpbin.org/get"
}

Attaching my sample proxy so that you can see what I am doing. javascript-rev1-2016-04-29.zip

This is not correct usage. It will work when everything goes well. Your code will not work correctly when there is an error in the request. Your code must check isSuccess() before calling getResponse(). Check the documentation.

getResponse() returns a valid value, if and only if the call was successful.

I believe it is a coding error on your part.

You cannot be sure that the call was successful, with the code written as it is.

The getResponse() function is documented as

getResponse(): (object) Returns the response object if the httpClient.send() was complete and successful. The returned object has the identical methods and properties as the content.proxyResponse object documented in the Apigee Edge Javascript object model.

Note: IF the call was successful .

To be correct, your code must check exchange.isSuccess() before calling exchange.getResponse().

I tested the code calling another API proxy API-B configured in APIGEE same environment, and i make a request to A and checked in trace of API-B to debug whether A calls B and gets response. So B actually sends a response back, but my exchange Object doesn't show anything. @Dino Any idea why is Exchange object is not loaded on an successfull response from endpoint.