Convert fault response from JSON to XML

Hi,

I have a scenario depending upon some parameter i have to send response as JSON or XML. for success response I no need to worry, but I getting problem for fault response. I have some faults on proxy request flow, configured some faults in proxy fault rules like invalid API key and target fault rules like timeout, service-unavailable.

Please suggest me how I can solve this problem.

0 3 322
3 REPLIES 3

@Veerendra Deshpande

You can add multiple steps in your faultrules with conditions. For eg:

<FaultRule name="API Key Faults">
<Condition>(oauthV2.Validate-API-Key.failed = true) </Condition>
  <Step>  <Name>VerifyAPIKey-FailedResponse</Name>  </Step> <!-- JSON response -->
  <Step>
   <Name>Convert-Faultresponse-To-XML</Name> <!-- JSON To XML Policy -->
   <Condition>(request.queryparam.foramt = "xml") </Condition>
  </Step>
 </FaultRule>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="VerifyAPIKey-FailedResponse">
    <DisplayName>VerifyAPIKey-FailedResponse</DisplayName>
    <FaultRules/>
    <Properties/>
    <Set>
        <Payload contentType="application/json">
                {"error":{
        "code": "400",
        "message": "Invalid API Key provided in x-api-key header"
    }}
            </Payload>
        <StatusCode>400</StatusCode>
        <ReasonPhrase>Bad Request</ReasonPhrase>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo type="response" transport="http" createNew="false"/>
</AssignMessage>

Thanks for your comment. This solution is fine when my all faults are raised from or configured in fault rules. but i have some fault it has throw from request flow in proxy-endpoint. how i have to handle those as well.

If you are referring to fault rules similar to system exceptions(unchecked) - you can configure a default fault rule and have the JSON-to-XML policy configured there