Search code examples
wso2wso2-api-manager

how to set json validator globally to check the Content Type of request that it must be json in wso2 if not 415 Unsupported Media Type as status code


I am using wso2 API manager 3.2.0 .I want to check incoming requests Content Type that it only allowed to be application/json if any other type sent as content type for example application/xml ,415 Unsupported Media Type as must be show as status code for all resources in a rest API.how can I do this.for example I sent following request and recieve 500 internal server error , my goal is to check content type and if content type was not application/json , statu code 415 Unsupported Media Type show to client as response :

curl --location --request POST 'https://10.10.10.24:8243/pri/general/v1/service1' \
--data ''

Solution

  • You can create a Custom Mediation Policy and engage the following logic.

    <filter regex="application/json" source="$trp:Content-Type">
        <then>
        </then>
        <else>
          <property value="415" name="HTTP_SC" scope="axis2"/>
          <respond/>
        </else>
    </filter>