XML to JSON policy failing when there is a 500 internal error

I am using XML to JSON policy in the proxy endpoints post flow.This is working fine, If the response from the back end is "200" with out any errors. But if, there is an error in the back end Its failing to convert the error response from XML to JSON and the status code is coming as "500 Internal Server Error" Is there any way I can convert the response from XML to JSON when there is back end error ??

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

Solved Solved
0 7 1,226
1 ACCEPTED SOLUTION

I think the answer from Arijit may be close, but not quite true.

if there is an error in the back end Its failing to convert the error response from XML to JSON and the status code is coming as "500 Internal Server Error"

I suspect that the XMLToJSON policy is placed in the normal flow. If there is a 500 error issued from the backend (aka upstream), the API Proxy normally enters a "Fault" state. This means normal API Proxy flow logic is interrupted and processing transfers to FaultRules. You can read about this here.

If the proxy enters fault state, then the XMLToJSON policy that normally executed, may not execute. This is true whether or not the content of the payload delivered by the backend is XML.

You have two options:

  • tell the target to treat 500 errors as non-faults. Do this with the success.codes setting on the target.
  • Attach the XMLToJSON policy also in your FaultRules

View solution in original post

7 REPLIES 7

Can you confirm if the 500 error response is returned as a customised string or xml from the backend?

You may try customising message for handling faults such as 5xx server errors. There is a list of fault codes under "Runtime errors" here https://docs.apigee.com/api-platform/reference/policies/xml-json-policy

You may need to use API monitoring to find the exact fault code you are facing, or write a handler for each one of them.

Here is a good step-by-step guide to configure fault handling https://docs.apigee.com/api-platform/fundamentals/fault-handling.html

500 server errors are not returned in xml format, hence not converted to JSON.

Not always true. Remember the 500 error is being returned by the backend. This backend can return any sort of content-type it likes, including XML.

See my answer for a different perspective on the problem.

@Dino-at-Google Thanks for correcting. You are right that messages can be customised in the backend. I've seen instances where people pass strings for the errors and policies don't work on them because of their format, hence said. However, I now realise that the language is misleading - suspicion conveyed as a rule. Updating my answer and upvoting yours!

Thanks

@Arijit Dutta

I think the answer from Arijit may be close, but not quite true.

if there is an error in the back end Its failing to convert the error response from XML to JSON and the status code is coming as "500 Internal Server Error"

I suspect that the XMLToJSON policy is placed in the normal flow. If there is a 500 error issued from the backend (aka upstream), the API Proxy normally enters a "Fault" state. This means normal API Proxy flow logic is interrupted and processing transfers to FaultRules. You can read about this here.

If the proxy enters fault state, then the XMLToJSON policy that normally executed, may not execute. This is true whether or not the content of the payload delivered by the backend is XML.

You have two options:

  • tell the target to treat 500 errors as non-faults. Do this with the success.codes setting on the target.
  • Attach the XMLToJSON policy also in your FaultRules

Thanks @Dino-at-Google

By setting success.codes, To treat 500 error as a success in Apigee, I was able get XMLToJSON working

<Properties> <Property name="success.codes">2XX,3XX,400,5XX</Property> </Properties>

in HTTP target connection. Thanks

Thanks hemanth.

To be accurate, the XMLToJSON policy was always "working". It was just that the control flow you had set up did not invoke the XMLToJSON policy in some cases, specifically in the case in which the target returned a 500 status. You changed your proxy logic so that even in that case, the XMLToJSON policy gets executed.