HttpClient request failing

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

Solved Solved
0 2 881
1 ACCEPTED SOLUTION

You wrote

However, I am inconsitent results, where I get back the JSON data that I expect some times.

It looks like you incorrectly using the callback mechanism in httpClient.

  var response = httpClient.get(url, onComplete);
  var ebsResponse = response.getResponse().content.asJSON;

This is not correct usage.

Your code that examines the response needs to be in the onComplete function. The correct usage is described here.

function onComplete(response,error)
   // Check if the HTTP request was successful
    if (response) {
      var r = response.getResponse();
      if (r && r.content) {
        var ebsResponse = r.content.asJSON;
        ...
      }
      else {
        // handle the case in which there is no content
      }
    } 
    else {
      // handle the case in which response is not available. 
    }
}

// Specify the asynchronous callback Function as an argument
httpClient.get("http://example.com", onComplete);

The onComplete function is invoked asynchronously with respect to the httpClient.get(). If you don't understand what this means, I suggest that you read up on asynchronous javascript.

Also, your code does not check whether the response.content is defined. There are many cases in which that may occur. I suggest that you will want to make your code a little more resilient , by checking for those cases. Add more error checking.

View solution in original post

2 REPLIES 2

Check the response payload and the content-type header. It should be application/Jon. The asJSON call seems to be failing as per the error message.

You wrote

However, I am inconsitent results, where I get back the JSON data that I expect some times.

It looks like you incorrectly using the callback mechanism in httpClient.

  var response = httpClient.get(url, onComplete);
  var ebsResponse = response.getResponse().content.asJSON;

This is not correct usage.

Your code that examines the response needs to be in the onComplete function. The correct usage is described here.

function onComplete(response,error)
   // Check if the HTTP request was successful
    if (response) {
      var r = response.getResponse();
      if (r && r.content) {
        var ebsResponse = r.content.asJSON;
        ...
      }
      else {
        // handle the case in which there is no content
      }
    } 
    else {
      // handle the case in which response is not available. 
    }
}

// Specify the asynchronous callback Function as an argument
httpClient.get("http://example.com", onComplete);

The onComplete function is invoked asynchronously with respect to the httpClient.get(). If you don't understand what this means, I suggest that you read up on asynchronous javascript.

Also, your code does not check whether the response.content is defined. There are many cases in which that may occur. I suggest that you will want to make your code a little more resilient , by checking for those cases. Add more error checking.