Js http client and 5xx and 4xx handling

Not applicable

I've attached a JS policy to one of my endpoints. In the policy, I call an URL that returns me some data and I add these data to the original response.

Something like

// Get the extra data from a thirdparty service
var fooData = httpClient.get('http://www.thirdparty.url/api');
fooData.waitForComplete();
// Here I wanna check if the response is a 200
if (fooData.isSuccess()) {
   // Some logic
   response.content = JSON.stringify(newContent);
}

AFAIK `isSuccess` is true also if the url http://www.thirdparty.url/api returns a 4xx or a 5xx error.

How do I check if the url returned a 200 ?

Solved Solved
0 1 1,809
1 ACCEPTED SOLUTION

hi @Simone Fumagalli as per the documentation, you can handle such scenario with response status codes;

http://docs.apigee.com/api-services/reference/javascript-object-model#makingjavascriptcalloutswithht..., check Example 5;

.....
// Wait for the asynchronous GET request to finish
  exchange.waitForComplete();

  // get the response object from the exchange
  response = exchange.getResponse();

  // get the HTTP status code from the response
  status = response.status;

  if (status == 200) {
    context.setVariable('userCheck.trace', 'user exists');
  }
.....

View solution in original post

1 REPLY 1

hi @Simone Fumagalli as per the documentation, you can handle such scenario with response status codes;

http://docs.apigee.com/api-services/reference/javascript-object-model#makingjavascriptcalloutswithht..., check Example 5;

.....
// Wait for the asynchronous GET request to finish
  exchange.waitForComplete();

  // get the response object from the exchange
  response = exchange.getResponse();

  // get the HTTP status code from the response
  status = response.status;

  if (status == 200) {
    context.setVariable('userCheck.trace', 'user exists');
  }
.....