Handling Response based on the type of request(xml/json/any other).

Hi, I am trying throw the error response based on the type of request. It works for JSON request’s & other plain requests. But giving error when I sent XML request to the Body.
Here “ProxyRequest” is a context variable containing Proxy request.
Kindly suggest is their any code to handle xml messages…

I think you should not try to convert from JSON to XML in a JavaScript call-out.

There’s a JSONToXML policy in Apigee that does this for you.

If I were doing this, I would conditionally add that policy , based on the request accept header. Do this in the proxy response flow. Something like this:

<PostFlow name='PostFlow'>     <Request>     </Request>     <Response>       <Step>         <Condition>request.header.accept = "application/xml" OR request.header.accept = "text/xml"</Condition>         <Name>J2X-Convert-Response-to-XML</Name>       </Step>     </Response>   </PostFlow> 

The Condition might need to be different if you want to match other Accept values. (For example, sometimes Accept will have several options, separated by a comma. In which case you would probably need to do a regex match looking for the application/xml or text/xml values in that Condition).

If it is a fault/error, then you would do similar, but attach the JsonToXML policy into a FaultRule, not in the PostFlow/Response.

1 Like