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! Go to 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'); } .....
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'); } .....