I am using HTTPClient to make multiple http requests to my backend. However, I am inconsitent results, where I get back the JSON data that I expect some times. I make a HTTPClient requests in the Javascript policy, so it is not too many. My back end is working fine.
var data = JSON.parse(context.getVariable("user.data"));
list = [];
function onComplete(response,error){
if (response) {
context.setVariable('example.status', response.status);
} else {
context.setVariable('example.error', 'Woops: ' + error);
}
}
try{
for(var i=0; i< data.length; i++){
var url = "http://backendurl";
var response = httpClient.get(url, onComplete);
var ebsResponse = response.getResponse().content.asJSON;
if(ebsResponse.isSuccess()){
if(!ebsResponse.unitPrice){
price = 300;
}
else{
price = ebsResponse.unitPrice
}
print(price)
list.push({
recordid : data[i].recordid,
userid : data[i].userid,
item_number: data[i].item_number,
partType : data[i].partType,
productName : data[i].productName,
productDescription : data[i].productDescription,
price: price
})
}
else if (ebsResponse.isError()) {
throw new Error(ebsResponse.getError());
}
}
context.setVariable("response.content", JSON.stringify(list));
}
catch(err){
response.content.asJSON.error = err;
}
Error:
{
"fault": {
"faultstring": "Execution of mashup failed with error: Javascript runtime error: \"TypeError: Cannot read property \"asJSON\" from undefined. (mashup.js:45)\"",
"detail": {
"errorcode": "steps.javascript.ScriptExecutionFailed"
}
}
}
I do not think this is an issue with the performance of my backend, but I am not too sure.
Thanks