Quantcast
Channel: Sanjeewa Malalgoda's Blog
Viewing all articles
Browse latest Browse all 220

How to send specific status code and message based on different authentication faliures WSO2 API Manager

$
0
0
In WSO2 API Manager all authentication faliures will hit auth failure handler. There you will be able to change message body, content, header based on internal error codes.
As example if we got resource not found error while doing token validation then Error Code will be 900906. So in same way we will have different error codes for different failures.

So in this sample will generate custom message for resource not found issues while doing token validation.
For this we will specifically check error code 900906 and then route request to specific sequence.

Please refer following sequence and change to auth_failure_handler to call sequence.

_auth_failure_handler_

<sequencename="_auth_failure_handler_"xmlns="http://ws.apache.org/ns/synapse">
    <propertyname="error_message_type"value="application/xml"/>   
    <filtersource="get-property('ERROR_CODE')"regex="900906">
      <then>
          <sequencekey="sample"/>
          <drop/> 
      </then>
      <else>        
      </else>
    </filter>
    <sequencekey="_build_"/>
</sequence>


sequence

<?xmlversion="1.0"encoding="UTF-8"?>
<sequencexmlns="http://ws.apache.org/ns/synapse"name="sample">
    <payloadFactorymedia-type="xml">
        <format>
            <am:faultxmlns:am="http://wso2.org/apimanager">  
                <am:message>Resource not found</am:message>
                <am:description>Wrong http method</am:description>
            </am:fault>
        </format>
    </payloadFactory>
    <propertyname="RESPONSE"value="true"/>
    <headername="To"action="remove"/>
    <propertyname="HTTP_SC"value="405"scope="axis2"/>
    <propertyname="messageType"value="application/xml"scope="axis2"/>
    <send/>  
</sequence>   

Viewing all articles
Browse latest Browse all 220

Trending Articles