Petteri Pertola
Petteri Pertola

Reputation: 281

WSO2 Integrator: HTTP Status 401 - how to get the error body?

We are using the latest available WSO2 Integrator v. 6.6.0.

I am calling a HTTP endpoint (REST API) from my sequence, and depending on the content of my message, the endpoint will respond either with HTTP 200 OK with a JSON response body, or HTTP 401 Unauthorized (also with a JSON response body - an error body detailing the problem).

For example:

{
    "error_reason": "Invalid identifier",
    "error_description": "You do not have access to the specified identifier",
    "http_status": 401,
    "error": "Unauthorized"
}

I need to access the error_description field of the returned error to return it to the original sender of the message in my sequence.

How can I do this using WSO2 Integrator?

Upvotes: 0

Views: 856

Answers (2)

sanzar rahman
sanzar rahman

Reputation: 1

Use this and then use filter to get payload and status code . This property won't take it to fault sequence.

<property name="non.error.http.status.codes" value="403" scope="axis2" type="STRING"/> 

Ref: https://ei.docs.wso2.com/en/latest/micro-integrator/references/mediators/property-reference/http-transport-properties/

Upvotes: 0

Arunan
Arunan

Reputation: 3426

You can use a filter mediator to check the HTTP Status code and write the logic. You can find a sample below.

<?xml version="1.0" encoding="UTF-8"?>
<api context="/mocky" name="Mocky" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <call>
                <endpoint>
                    <http method="GET" uri-template="https://run.mocky.io/v3/570d617a-0b39-4ca1-b209-60d4b9abadb5">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>-1</progressionFactor>
                            <maximumDuration>0</maximumDuration>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </http>
                </endpoint>
            </call>
            <filter regex="401" source="$axis2:HTTP_SC">
                <then>
                    <payloadFactory media-type="json">
                        <format>{"message" :  "$1"}</format>
                        <args>
                            <arg evaluator="json" expression="$.error_description"/>
                        </args>
                    </payloadFactory>
                    <respond/>
                </then>
                <else>
                    <payloadFactory media-type="json">
                        <format>{"message" :  "Happy Path"}</format>
                        <args/>
                    </payloadFactory>
                    <respond/>
                </else>
            </filter>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Upvotes: 0

Related Questions