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

huangapple go评论47阅读模式
英文:

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?

英文:

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,

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

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?

答案1

得分: 1

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.

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

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/

英文:

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.

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

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/

huangapple
  • 本文由 发表于 2023年3月31日 18:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定