我们正在使用最新的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执行此操作?
您可以使用过滤器介体来检查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>