WSO2 ESB 5.0.0 - How to check API response is in JSON format or not

Viewed 36

How to validate the API response is in JSON or in HTML format using outsequence in wso2 ESB 5.0.0 I want to alter the response code if response is in HTML format.

1 Answers

You can try use switchMediator with something like that below. But be aware and dont try to use <log level =" full "> on HTML, unless you have proper messageBuilder and messageFormater for that content type. In default WSO2 ESB tryes to convert using xml builder, and it will ended with error. I tried this on wso2ei 6.5.0 but i believe it should work on esb 5.0.0.

<switch source="$trp:Content-Type">
     <case regex="^(application\/(json|x-javascript))(;.*)?$">
        <log>
           <property name="Looks like Json format" expression="$trp:Content-type"/>
        </log>
     </case>
     <case regex="^(text\/html)(;.*)?$">
        <!--Don't log full, unless you have proper axis2.xml configuration for that contentType-->
        <log level="custom">
           <property name="Looks like HTML format" expression="$trp:Content-type"/>
        </log>
     </case>
     <default>
        <log level="custom">
           <property name="Unknown format" expression="$trp:Content-type"/>
        </log>
     </default>
 </switch>
Related