How do I send JSON body to my POST request in Javascript and same way do do I get JSON response in my APIGEE api proxy program

Not applicable

Hello,

I am creating api proxy program where I can send bunch of POST request and GET request. With those request I have to send JSON body with bunch of key-values pairs and in return I get JSON response where also key-values pair comes .

Please guide me If there is and build in policy is there or how do I do in JavaScript policy.

Please share sample code or example for understand format.

Thank you.

Solved Solved
0 6 46.1K
1 ACCEPTED SOLUTION

Not applicable

Here is a sample that posts an entry to Splunk using an async POST method.

var splunkLoggingServerURL = context.getVariable("logConfig.splunkLoggingServerURL");
var splunkLoggingServerBasicAuth = context.getVariable("logConfig.splunkLoggingServerBasicAuth");

var logJsonObj = JSON.parse(context.getVariable("log"));
logJsonObj.foo = "bar";

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

var myRequest = new Request(splunkLoggingServerURL, "POST", headers, JSON.stringify(logJsonObj));

// asynchronous POST for performance reasons
httpClient.send(myRequest);

The sample gets a log message from context, parses it out, adds additional attributes, stringifies the modified object, then performs the POST.

View solution in original post

6 REPLIES 6

Hi Dhwani,

It's possible that the httpClient (Javascript callout) feature is what you are looking for. The Javascript httpClient object lets you make HTTP requests to backend targets in Javascript and retrieve the JSON responses. (See in particular "Example 3" in the following topic — it shows how to do a POST request and receive a JSON response).

http://apigee.com/docs/api-services/reference/javascript-object-model#makingjavascriptcalloutswithht...

Here are other doc links that might be helpful with respect to using Javascript in a proxy:

* Example proxy using the JavaScript policy to execute custom code (very simple case):

http://apigee.com/docs/api-services/cookbook/programming-api-proxies-javascript

* The Javascript Object Model reference. It explains how to work with Javascript objects in an API proxy flow.

http://apigee.com/docs/api-services/reference/javascript-object-model

* The Javascript policy. It explains how to use the Javascript policy to specify custom Javascript that you want to execute in a proxy flow:

http://apigee.com/docs/api-services/reference/javascript-policy

Thank you @wwitman

I am looking for example where POST/GET/PUT/DELETE request have json body with request and in response I will get json response. Special I would like to know how to send body during request. Something like json payload.

Please share with me if you have anything on this topic.

Thank you. :-)

Hi, here's how you can make a POST request with a JSON body to an Apigee API proxy. It assumes you have a proxy that's deployed on Apigee, and that it accepts the given POST data on an endpoint called "/myendpoint".

curl -X POST -H "Content-Type: application/json" -d '{ “greeting”:”hello world” }' "http://myorg-myenv.apigee.net/myendpoint”

You can access the response data from the backend target in a Javascript policy in the context.targetResponse object as explained here http://apigee.com/docs/api-services/reference/javascript-object-model. You can also use an ExtractVariables policy to grab the response data.

I hope this helps, but if not please add some more information about what you're trying to do.

Regards,

Will

Not applicable

Here is a sample that posts an entry to Splunk using an async POST method.

var splunkLoggingServerURL = context.getVariable("logConfig.splunkLoggingServerURL");
var splunkLoggingServerBasicAuth = context.getVariable("logConfig.splunkLoggingServerBasicAuth");

var logJsonObj = JSON.parse(context.getVariable("log"));
logJsonObj.foo = "bar";

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

var myRequest = new Request(splunkLoggingServerURL, "POST", headers, JSON.stringify(logJsonObj));

// asynchronous POST for performance reasons
httpClient.send(myRequest);

The sample gets a log message from context, parses it out, adds additional attributes, stringifies the modified object, then performs the POST.

For cases where you want to read and process the response from the httpClient you can use the following:

var myResult = httpClient.send(myRequest);

if (sync) {
    // Wait for the asynchronous POST request to finish
    myResult.waitForComplete();
    // get the response object from the exchange
    var response = JSON.parse(myResult.getResponse());
  
    //do something interesting with the response object
    response.foo="bar";

    // expose the response as a flow variable
    context.setVariable("myResponse", JSON.stringify(response));
}

You would want to add error handling and the like to make this more robust (say you got a 503 on the response). But I think you get the gist of what to do here...

Thanks for sharing this. I need to POST to an https listener so just changed the URL used. Then, I realised that although the requests are received at the other end, they contain no body. Is there a different way to make the call over https?