HTTP client to get response from post call using javascript

amotupalli
Participant I

Here is my code.

var SchCodeRequest = '{ "aa" : "","abb" : ""}';
var req = JSON.parse((JSON.stringify(SchCodeRequest)));
var url = "https://localHost";
var operation = "POST";
var headers = { 'Content-Type' : 'application/json' };
var request = new Request(url, operation, headers, req);
print(request);
var exchange = httpClient.send(request);
if (httpres.isSuccess()) 
{ 
  var resp = exchange.getResponse();
  print(resp);
  var hresp = JSON.parse(resp);
  context.setVariable("finalResponse", hresp);
  //var responsePayload = exchange.getResponse().content;
}
else 
{
  var resp = httpres.getError();
  context.setVariable("httpres", resp); 
}

when I try to hit the request it printing as below request:https://localHost? But the expected output should be like below https://localHost

0 6 1,673
6 REPLIES 6

yes, thanks for the code. I don't see what response you're getting, or what you're expecting to get.

One thing I'll point out. This code section looks wrong

var SchCodeRequest = '{ "aa" : "","abb" : ""}';
var req = JSON.parse((JSON.stringify(SchCodeRequest)));

... in a couple ways.

First, invoking JSON.parse() on the output of JSON.stringify() is basically a no-op, if I'm not mistaken. They are inverse functions, so you will just get the original argument out.

foo == JSON.parse(JSON.stringify(foo)) // always true

So it seems pointless to stringify, then parse the SchCodeRequest thing in your 2nd line.

Getting to the intent of your code, It seems like you want the request payload to be json. In that case, I suggest you initialize a JS object, and then serialize it with JSON.stringify.

var SchCodeRequest = { "aa" : "","abb" : ""}; // no single quotes wrapping this
var payload = JSON.stringify(SchCodeRequest);

Your code has

var SchCodeRequest = '{ "aa" : "","abb" : ""}'; // this is a string

The right-hand-side is wrapped in single quotes. That means it's a string. The string just happens to have somethiing that looks like JSON within it. But it's still a string.

On the other hand, this version without quotes:

var SchCodeRequest = { "aa" : "","abb" : ""};  // this is an object

...initializes an object. It has a couple properties, aa and abb.

If you then call JSON.stringify() on that object you will get ... a string.... similar to the quoted string in your original code.

ok, aside from that payload detail, let me say that I am not at all clear on the problem you're experiencing. I think the output you're seeing is not what you're expecting, but I don't see examples of either of those in your post.

But I want to point out something else. You're trying to invoke "https://localhost". What is your intent with that? Maybe you have a server running on your local developer workstation and you want an Apigee JS policy to contact that server? If that's your intent, it won't work. The Apigee JS policy runs on the message processor machine, which is in the Apigee cloud. (1) It won't have connectivity to your local development workstation, and (2) it won't be able to resolve "localhost" to your workstation even if connectivity WAS possible.

So you may need to rethink what you're trying there.

One final recommendation: use the onComplete callback for the HTTP client.

  function onComplete(response, error) {
    if (response) {
      context.setVariable('example.status', response.status);
// response.content here holds the body } else { context.setVariable('example.error', 'Whoops: ' + error); } } var url = 'https://whatever.example.com/foo/bar'; // not localhost! var payload = JSON.stringify({ foo : 'bar', whatever : 1234 }); var headers = { 'Content-Type' : 'application/json', 'Authorization' : 'Bearer xyz' }; var req = new Request(url, 'POST', headers, payload); httpClient.send(req, onComplete);

Good luck on your explorations.

Thank You Dino,

For your response,

Acutally we are trying to call backend through javascript because we have to call same request for 5 times.

We have tried your code. But as a response it is giving Timeout.

Can you please help this.

Yes, the JS callout is time limited.

You may need to extend the time limit attribute on the JS policy.

<Javascript name='JS-1' timeLimit='2200' >
...

Also, consider sending out the 5 calls in parallel, to make the overall time lower.

Hello Dino,

Timeout issue is resolved.

The endpoint which we are hitting is giving response But in Apigee we are getting response as empty

Please help us on this

Hello Dino,

When we trying to print the res (which you have mentioned in the above code) it is showing below

request:https://example/gdt?

But to the backend it show go as

https://example/gdt

That is the reason we getting empty as a response from backend.

Could you please help on this.

Are you telling me that your backend requires an empty query string in order to function properly? I don't know if the httpClient within the JavaScript callout can produce an empty query string. I don't have a suggestion for solving that.