在 WSO2 ESB 中将响应从一个调用调解器传递到另一个调解器
Pass responses from one call mediator to another in WSO2 ESB
我使用 Integrator 工具创建了一个 REST API。我可以连续调用,但我不知道如何存储和使用第一次调用的响应作为 header 来调用下一个端点。
场景是:我必须首先调用一个端点来获取一个令牌,响应我想设置一个 header 和一个不记名令牌来检索一个 JSON 文件调用下一个端点。
我的代码:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/example" name="example" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET">
<inSequence>
<log description="request log" level="full"/>
<payloadFactory description="body" media-type="json">
<format>
{
"grant_type": "authorization_code"
}
</format>
<args/>
</payloadFactory>
<call>
<endpoint>
<http method="post" statistics="enable" trace="enable" uri-template="http://localhost:5000/token">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
<property name="grant_type" scope="transport" value="authorization_code"/>
</endpoint>
</call>
<filter regex="200" source="get-property('axis2', 'HTTP_SC')">
<then>
<log level="custom">
<property name="switchlog" value="Case: first call successful"/>
</log>
<call>
<endpoint>
<http method="get" uri-template="http://localhost:5000/json">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<respond description="final"/>
</then>
<else>
<log level="custom">
<property name="switchlog" value="Case: first call unsuccessful"/>
</log>
<respond/>
</else>
</filter>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
我该怎么做?
您可以在第一次调用后将令牌存储在注册表中。它将允许请求的其余部分使用令牌调用返回。请参阅 Gmail 连接器实现。这里我们使用脚本中介器将令牌存储在注册表中。 (如果我们在注册表中有一个有效的令牌,则需要过滤逻辑来跳过令牌端点的调用,如果令牌已过期,我们也应该更新令牌)
希望这个示例调解会有所帮助,
我使用 Integrator 工具创建了一个 REST API。我可以连续调用,但我不知道如何存储和使用第一次调用的响应作为 header 来调用下一个端点。
场景是:我必须首先调用一个端点来获取一个令牌,响应我想设置一个 header 和一个不记名令牌来检索一个 JSON 文件调用下一个端点。
我的代码:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/example" name="example" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET">
<inSequence>
<log description="request log" level="full"/>
<payloadFactory description="body" media-type="json">
<format>
{
"grant_type": "authorization_code"
}
</format>
<args/>
</payloadFactory>
<call>
<endpoint>
<http method="post" statistics="enable" trace="enable" uri-template="http://localhost:5000/token">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
<property name="grant_type" scope="transport" value="authorization_code"/>
</endpoint>
</call>
<filter regex="200" source="get-property('axis2', 'HTTP_SC')">
<then>
<log level="custom">
<property name="switchlog" value="Case: first call successful"/>
</log>
<call>
<endpoint>
<http method="get" uri-template="http://localhost:5000/json">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<respond description="final"/>
</then>
<else>
<log level="custom">
<property name="switchlog" value="Case: first call unsuccessful"/>
</log>
<respond/>
</else>
</filter>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
我该怎么做?
您可以在第一次调用后将令牌存储在注册表中。它将允许请求的其余部分使用令牌调用返回。请参阅 Gmail 连接器实现。这里我们使用脚本中介器将令牌存储在注册表中。 (如果我们在注册表中有一个有效的令牌,则需要过滤逻辑来跳过令牌端点的调用,如果令牌已过期,我们也应该更新令牌) 希望这个示例调解会有所帮助,