【问题标题】:Pass responses from one call mediator to another in WSO2 ESB在 WSO2 ESB 中将响应从一个呼叫中介传递到另一个
【发布时间】:2020-11-21 10:00:21
【问题描述】:

我使用 Integrator 工具创建了一个 REST API。我可以连续调用,但我不知道如何存储和使用第一次调用的响应作为标题来调用下一个端点。

场景是:我必须首先调用一个端点来获取一个令牌,响应我想设置一个带有承载令牌的标头来检索一个调用下一​​个端点的 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>

我该怎么做?

【问题讨论】:

    标签: wso2 wso2esb wso2-am


    【解决方案1】:

    您可以在第一次调用后将令牌存储在注册表中。它将允许请求的其余部分使用令牌调用 back。请参阅 Gmail 连接器实现。在这里,我们使用脚本中介将令牌存储在注册表中。 (如果我们在注册表中有有效令牌,则需要过滤逻辑来跳过令牌端点的调用,如果令牌过期,我们也应该更新令牌) 希望这个示例中介会有所帮助,

    https://github.com/wso2-extensions/esb-connector-gmail/blob/master/src/main/resources/config/getAccessTokenFromRefreshToken.xml

    【讨论】:

    • 感谢您的回复。但是如果来自端点的响应不在 URI 中,你如何访问该值?在我的情况下,是在我从第一次调用中获得的有效负载内。访问令牌将在所有流程后被删除?
    • 如果令牌在响应负载中,可以使用带有 XPath 表达式的属性来提取它。例如:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多