WSO2集成商:HTTP状态401-如何获取错误正文?

问题描述 投票:0回答:1

我们正在使用最新的WSO2 Integrator v。6.6.0

我正在从序列中调用HTTP终结点(REST API),并且根据消息的内容,终结点将使用JSON响应正文通过HTTP 200 OK进行响应,或HTTP 401未经授权(也使用JSON响应正文-详细说明问题的错误正文)

例如:

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

我需要访问所返回错误的error_description字段,以将其返回给按顺序显示的消息的原始发件人。

我如何使用WSO2 Integrator执行此操作?

wso2 wso2esb wso2carbon wso2ei
1个回答
0
投票

您可以使用过滤器介体来检查HTTP状态代码并编写逻辑。您可以在下面找到一个示例。

<?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>
© www.soinside.com 2019 - 2024. All rights reserved.