Search code examples
dynamicwso2tokenwso2-enterprise-integratormediator

WSO2 EI: Can I use mediator to request another API and pass its response to the body request?


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.


Solution

  • 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.

    A sample mediation sequence will be as following

    <?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.