We are trying to make RESTful POST request from JavaScript policy in an Apigee proxy to talk to our service endpoint and having trouble making it work. Our JavaScript code follows the pattern found in api-platform-samples/sample-proxies/outbound-oauth/apiproxy/resources/jsc/api-token-get.js.
From TRACE tab, I found this line
print('responseObj: ' + JSON.stringify(responseObj));
outputs:
responseObj: undefined
We tested the endpoint itself with Postman and verified it works fine. Is the way httpClient is used in the code correct?
try
{
var key = context.getVariable(<variable name in string here>)
var user = context.getVariable(<variable name in string here>)
var password = context.getVariable(<vvariable name in string here>)
url = <RESTful URL here>
...
var bodyObj = {
'Key': key,
'User': user,
'Password': password,
};
var req = new Request(url, 'POST', {}, serializeQuery(bodyObj));
var exchange = httpClient.send(req);
// Wait for the asynchronous POST request to finish
exchange.waitForComplete(5000);
if(exchange)
{
if (exchange.isSuccess())
{
var responseObj = exchange.getResponse().content.asJSON;
print('responseObj: ' + JSON.stringify(responseObj));
...
}
catch (err) {
// Code to handle any error that may have happened previously by generating a response
...
}
function serializeQuery(obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
Solved! Go to Solution.
Thanks for the quick replies again!
Redirect issue: It turned out that the cause was WCF's behavior. We removed the trailing "/" from UriTemplate. Then redirect to a node happened without further update to the JavaScript code.
Timeout issue: javascript-executionTime was showing less than 200 ms (default value), but Postman call which invoked our Apigee proxy showed this error: "Execution of CustomerKeyToCustomerID2 failed with error: Javascript runtime exceeded limit of 200ms". So I bumped up timeLimit in JS policy to 2000 ms. And the timeout issue is resolved.
Reply deletion: I do not see Delete option. But I see Mute option. Muted replies are visible from me (of course. I may chose to unmute them later), but I wonder if they are hidden from you now.
Anyway I will close this ticket. Thanks for your help, especially showing me a best-practice model of httpClient usage!