Apigee Http.client call

Hi team,

Can we make http.client call through Java script now? I have to implement async and parallel calls in Apigee. I tried with http.client call but it is not reaching backend. I am assuming it might be deprecated along with node js . @dchiesa1 

Pls help

0 3 99
3 REPLIES 3

Can we make http.client call through Java script now?

yes

What problem are you having? What code are you using?

Hi @dchiesa1 ,

Thanks for your response, Pls find the details.

Apigee is making fire and forget call through http client in javascript. But request is not reaching the backend server. My code is as below, isSuccess variable I am getting as success and not receiving any error. Kindly help.

var requestPayload = '{"custId" : "12345";

var req = JSON.parse(JSON.stringify(requestPayload));

var url = ""<URL removed by staff>

var operation = "POST";

var headers = { 'Content-Type' : 'application/json};

var request= new Request(url, operation, headers, req);

var exchange = httpClient.send(request);

var isReqSuccess = exchange. is success( );

 

 

Hi Sindhu

I think you're not waiting for the request to complete. 

By calling httpClient.send(), you are requesting that Apigee send the request. Apigee sends it asynchronously. At the completion of httpClient.send(), the request has not yet been sent. If you terminate your JS code at that point, Apigee never sends the request.

You can avoid this by setting a callback. The corrected code should look something like this: 

 

  function onComplete(response, error) {
    if (response) {
      context.setVariable('jscallout-http-response-status', response.status);
      // response.content <== content
      // response.headers <== response headers
    }
    else {
      context.setVariable('jscallout-http-error', 'Whoops: ' + error);
    }
  }

  var url = 'https://url-removed-by-staff.com/seg1/seg2';
  var method = 'POST';
  var headers = { 'ID-Index' : 23, 'content-type': 'application/json' };
  var jsonToSend = { field1: 'value1', field2: true, ... };
  var body = JSON.stringify(jsonToSend);
  var req = new Request(url, method, headers, body);
  httpClient.send(req, onComplete);
  // the onComplete fn is called when the response is received.

 

It is an anti-pattern to use the exchange object. Use the callback.