WSO2 EI:我可以使用中介来请求另一个 API 并将其响应传递给正文请求吗? [英] WSO2 EI: Can I use mediator to request another API and pass its response to the body request?

查看:37
本文介绍了WSO2 EI:我可以使用中介来请求另一个 API 并将其响应传递给正文请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我而言,我想将动态值(Bearer"+ {access-token})添加到标头 mediator .所以在头中介之前,我想调用一个 get-token API 并从它的响应中提取 {access-token} 元素.我怎样才能得到那个?非常感谢.

In my case, I want to add a dynamic value ("Bearer" + {access-token}) to the header mediator . So before the header mediator, I want to invoke a get-token API and extract {access-token} element from its response. How can I get that ? Thank you so much.

推荐答案

您可以使用中介序列来满足此类要求.您可以参考此博客了解更多详细说明关于如何根据您的要求开发序列.该博客是为 API Manager 产品编写的,但您仍然可以按照相同的步骤在 EI 中完成它.

You can achieve such requirements with mediation sequences. You can refer to this blog for more detailed instructions on how to develop a sequence for your requirement. The blog is written for the API Manager product, but nevertheless, you can follow the same to get it done in the EI.

示例调解序列如下

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="oauth2-sequence" xmlns="http://ws.apache.org/ns/synapse">
    <!-- token generation to the oauth server's token endpoint -->
    <!-- add the base64 encoded credentials -->
    <property name="client-authorization-header" scope="default" type="STRING" value="MDZsZ3BTMnh0enRhOXBsaXZGUzliMnk4aEZFYTpmdE4yWTdLcnE2SWRsenBmZ1RuTVU1bkxjUFFh" />
    <property name="request-body" expression="json-eval($)" scope="default" type="STRING" />
    <property name="resource" expression="get-property('axis2', 'REST_URL_POSTFIX')" scope="default" type="STRING" />

    <!-- creating a request payload for client_credentials -->
    <payloadFactory media-type="xml">
        <format>
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                <soapenv:Body>
                    <root xmlns="">
                        <grant_type>client_credentials</grant_type>
                    </root>
                </soapenv:Body>
            </soapenv:Envelope>
        </format>
        <args></args>
    </payloadFactory>

    <!-- set related headers to call the token endpoint -->
    <header name="Authorization" expression="fn:concat('Basic ', get-property('client-authorization-header'))" scope="transport" />
    <header name="Content-Type" value="application/x-www-form-urlencoded" scope="transport" />
    <property name="messageType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING" />
    <property name="REST_URL_POSTFIX" value="" scope="axis2" type="STRING" />

    <!-- change the token endpoint -->
    <call blocking="true">
        <endpoint>
            <http method="POST" uri-template="https://localhost:9443/oauth2/token" />
        </endpoint>
    </call>

    <!-- append the acquired access token and make the call to the backend service -->
    <property name="bearer-token" expression="json-eval($.access_token)" scope="default" type="STRING" />
    <property name="REST_URL_POSTFIX" expression="get-property('resource')" scope="axis2" type="STRING" />
    <header name="Authorization" expression="fn:concat('Bearer ', get-property('bearer-token'))" scope="transport" />
    <payloadFactory media-type="json">
        <format>$1</format>
        <args>
            <arg evaluator="xml" expression="get-property('request-body')" />
        </args>
    </payloadFactory>

    <!-- perform a send or call to complete the execution of the backend service call in EI -->
</sequence>

希望这有助于您开始实施.

Hope this helps you to start with implementation.

这篇关于WSO2 EI:我可以使用中介来请求另一个 API 并将其响应传递给正文请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆