How the get only one specific response in apigee?

Hi All,

I have a usecase where I have to send an HTTP request with an XML and JSON body. In the API Proxy, I would like to add an element (sum of two elements i.e, a & b) and then obtain the modified body only c as a response by adding the value of a & b.

My XML request by PostMan is:

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

My JSON request by PostMan is:

{"Add":{"a":"40","b":"70","c":"Total"}}

JavaScript code for adding two elements a and b and to print only "total as a response":

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

Expected Output:(Only addition of 2 nos) i.e,

Total = 110

But My Postman response Is :

{"fault":{"faultstring":"Execution of JavaScript-1 failed with error: Javascript runtime error: \"TypeError: Cannot read property \"Add\" from undefined. (JavaScript-1.js:2)\"","detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"}}}

Could someone please help me?

Any Advice what i'm doing wrong here?

Thanks and Regards

Govind Verma

@Anil Sagar @ Google, @Dino-at-Google,@Brendan, @deboraelkin,@Siddharth Barahalikar,@Priyadarshi Ajitav Jena,@Robert Johnson,@Nisha Mallesh,@Anil Sagar,@sudheendras,@Mukundha Madhavan,@Jeremy Whitlock,@Nagashree B

0 8 2,551
8 REPLIES 8

The thing is you need to parse the XML. You can do something like request.body.asXML

OR

simply use an Xml2Json policy and then use Javascript to parse Json. I hope you know how to parse and add json from one of your previous questions.

In Xml2Json policy change the Source to request and then in JS policy is the output variable of previous Xml2Json policy.

I am try to post the code from my laptop. Typing code is hard from mobile device.

Govind, I think you asked about this topic previously.

Isn't this the same question?: https://community.apigee.com/questions/65898/i-want-to-modify-the-xml-or-json-request-body-usin.html

And the answer you got there, with some example code, still applies here.

Hey

@Dino-at-Google

Thanks for you reply ,

In my previous question, When i'm sending request as :

{<br>  "Add": {<br>  "a": 10,<br>  "b": 20,<br>  "c": "Total",<br>  "target": "json"<br>  }<br>}

Then my response data is :

{<br>  "args": {},<br>  "data": "{\"Add\":{\"a\":10,\"b\":21,\"c\":31.0,\"target\":\"json\"}}",<br>  "files": {},<br>  "form": {},<br>  "headers": {<br>  "Accept": "*/*",<br>  "Accept-Encoding": "gzip, deflate",<br>  "Accept-Language": "en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7",<br>  "Cache-Control": "no-cache",<br>  "Content-Length": "48",<br>  "Content-Type": "application/json",<br>  "Host": "httpbin.org",<br>  "Origin": "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop",<br>  "Postman-Token": "f10655ba-8b80-2621-b509-0a504bf72b51",<br>  "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"<br>  },<br>  "json": {<br>  "Add": {<br>  "a": 10,<br>  "b": 20,<br>  "c": 30,<br>  "target": "json"<br>  }<br>  },<br>  "origin": "219.91.173.188, 104.154.179.1, 219.91.173.188",<br>  "url": "https://httpbin.org/post"<br>}

Which is correct, But I need clean output as :

c : 30<br>

In your previous example you were using httpbin.org/post as your target endpoint and so you were seeing that response in Postman.

In JS code used gives you the value of c.

Now it is upto you on how you use this response. You can add an AssignMessage policy on response side and set the payload coming from JS.

First, the reason your response looks that way is that you are pointing your API proxy to httpbin.org. httpbin.org is an echo service; it produces a JSON response representing the request that it received. You can see that the json response you showed includes fields for form data (empty), headers (a bunch of request headers are shown), origin, and URL.

If you don't want your api proxy to return that, then... don't configure your API proxy to forward requests to httpbin.org !

You may not be aware that this is happening. Here's a review of the fundamentals:

https://docs.apigee.com/api-platform/fundamentals/understanding-apis-and-api-proxies

Your API Proxy is connecting to a backend system, that YOU specified.

In your proxy endpoint, you have something like this:


  <RouteRule name="default">
    <TargetEndpoint>target-1</TargetEndpoint>
  </RouteRule>



If you don't want to connect to a backend, you must change it to something like this:

  <RouteRule name="NoRouteRule"/>

The name is not important. The reference to the TargetEndpoint is what is important. If you remove the reference to the TargetEndpoint, your API Proxy will no longer forward requests to the backend.

SECONDLY, if you want the response to be plain text, then instead of this (as suggested in Siddharth's answer to your prior Q)

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

you must use something like this:

var body = request.content.asJSON;
var c = body.Add.a + body.Add.b;
response.content = "c: " c;
context.setVariable('response.header.content-type', 'text/plain');

The former is augmenting the response - adding to it. The latter is replacing the response.

Hey @Siddharth Barahalikar

Thanks for you reply ,

Did you mean i need to add 2 JavaScript Policy to Parse Json and Xml data?

First JS policy as :

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

Second JS policy as :

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

And XmlToJson Policy as :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><br><XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-1"><br>  <DisplayName>XML to JSON-1</DisplayName><br>  <Properties/><br>  <Format>yahoo</Format><br>  <OutputVariable>response</OutputVariable><br>  <Source>request</Source><br></XMLToJSON>

But Unfortunately it's giving error message And Here i don't want to print whole response body, I

need clean output as :

c : 30

No. Not two policies.

Read my earlier reply. Try that. One JS policy.

Modify the RouteRule.