Search code examples
wso2wso2-esbwso2-api-managerwso2-micro-integrator

In the API Manager custom policy, I need to call an endpoint and the response need to store in the Property Mediator


My use case is, I need to create a custom API Manager mediation policy that will call an endpoint and the response from this endpoint needs to be stored in the header mediator and the incoming request will call the original backend endpoint after processing the mediation policy in the API Manager.

The issue I am facing currently is, I am not able to use send or call mediator, only the header mediator works, and if I use the header mediator with "To" not able to store the response. Attaching the sample code for reference,

    <?xml version="1.0" encoding="UTF-8"?>
<sequence name="UserToken" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <log>
        <property name="[User Token] : " value="Entered into sequence"/>
    </log>
    <property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
    <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>
    <header name="To" scope="default" value="https://22.33.444.55:8080/oauth/token?grant_type=client_credentials"/>
    <log>
        <property expression="$body" name="Response"/>
    </log>
</sequence>

Tried the same above code with Call Mediator as well instead of Header mediator but still no luck not able to log value.

Can someone help with this?


Solution

  • The Call mediator in non-blocking mode and Send mediator are not usable within custom sequences. Please refer Changing the Default Mediation Flow of API Requests for more information.

    Therefore you will need to use the Call mediator in blocking mode in the custom sequence as follows.

    <?xml version="1.0" encoding="UTF-8"?>
    <sequence name="UserToken" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
        <log>
            <property name="[User Token] : " value="Entered into sequence"/>
        </log>
        <property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
        <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>
        <call blocking="true">
            <endpoint>
                <http method="get" uri-template="https://22.33.444.55:8080/oauth/token?grant_type=client_credentials"/>
            </endpoint>
        </call>
        <log>
            <property expression="$body" name="Response"/>
        </log>
    </sequence>
    

    Update: If the backend endpoint is protected with OAuth, you can pass the credentials to the Endpoint security configuration which will generate the token and attach to the header.

    OAuth 2.0 Endpoint Security of APIM supports the following grant types,

    1. Client Credentials
    2. Resource Owner Password

    Depending on the grant type, you will need to define the required parameters to generate the token.

    If you need to send additional parameters to generate a token, you can add them using the Add Parameter option in the Endpoint security configuration UI. Refer to the below linked documentation for more information.

    https://apim.docs.wso2.com/en/latest/design/endpoints/endpoint-security/oauth-2.0/