I want to modify the xml or json request body using Javascript.

Hi All,

I have a usecase where I need to send an HTTP request with an XML or JSON body. In the API Proxy, I would like to add an element (sum of two other elements) and then obtain the modified body.

My XML request is:

<?xml version="1.0" encoding="utf-8"?>
<Add>
 <a>10</a>
 <b>20</b>
 <c>Total</c>
</Add> 

My JSON request is:

{
   "Add": {
      "a": "10",
      "b": "20",
      "c": "Total"
   }
} 

JavaScript code for adding two elements a and b:

var a=context.getVariable("request.body.Add.a");
var b=context.getVariable("request.body.Add.b");
//var a=context.getVariable("request.queryparam.a.N");
print("a"+a);
print("b"+b);
var Total = parseInt(a) + parseInt(b);
context.setVariable("request.body.c",Total); 

In Trace tool i'm getting 200 as a response but unable to get proper output :

Output from all Transactions:

anull
bnull 

Postman response Is :

{
  "args": {}, 
  "data": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Add>\n <a>10</a>\n <b>20</b>\n <c>Total</c>\n</Add>\n", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Cache-Control": "no-cache", 
    "Content-Length": "88", 
    "Content-Type": "application/xml", 
    "Host": "httpbin.org", 
    "Postman-Token": "690d3cd0-de25-44af-9451-8aec1945c2", 
    "User-Agent": "PostmanRuntime/6.4.1"
  }, 
  "json": null, 
  "origin": "114.79.146.115, 104.154.179.1, 114.79.146.115", 
  "url": "https://httpbin.org/post"
}

Could someone please help me?

Any Advice what i'm doing wrong here?

Thanks and Regards

Govind Verma

Solved Solved
0 4 3,835
2 ACCEPTED SOLUTIONS

Hi @Govind Verma

You first need to access the request as JSON, then perform the calculations.

See the JavaScript object model docs for more details.

For JSON request if you use:

{
   "Add": {
      "a": 10,
      "b": 21,
      "c": "Total"
   }
} 

Then the JavaScript policy, placed in the response flow of a no-route proxy, could be as simple as:

var body = request.content.asJSON;
body.Add.c = body.Add.a + body.Add.b;
response.content.asJSON = body;

I'll leave it to you to deal with XML vs JSON.

Hope that helps

View solution in original post

Govind what is the error you are getting while using Kurt's answer?

It should work, I used same code with some modification, try this,

var body = request.content.asJSON;
body.Add.c = body.Add.a + body.Add.b;
context.setVariable("total",JSON.stringify(body.Add.c));

View solution in original post

4 REPLIES 4

Hi @Govind Verma

You first need to access the request as JSON, then perform the calculations.

See the JavaScript object model docs for more details.

For JSON request if you use:

{
   "Add": {
      "a": 10,
      "b": 21,
      "c": "Total"
   }
} 

Then the JavaScript policy, placed in the response flow of a no-route proxy, could be as simple as:

var body = request.content.asJSON;
body.Add.c = body.Add.a + body.Add.b;
response.content.asJSON = body;

I'll leave it to you to deal with XML vs JSON.

Hope that helps

Govind what is the error you are getting while using Kurt's answer?

It should work, I used same code with some modification, try this,

var body = request.content.asJSON;
body.Add.c = body.Add.a + body.Add.b;
context.setVariable("total",JSON.stringify(body.Add.c));

Thanks @Siddharth Barahalikar for your reply, I got my answer.