Not able to call Dynamic Endpoints/URLs on the basis of Message Mediation polices in WSO2 API Manager

后端 未结 1 1269
生来不讨喜
生来不讨喜 2021-01-28 00:50

I\'m using APIM-3.1.0 and I need to Redirect APIs’ based upon header or request parameter. I have tried for request parameter but unable to call different API\'s. I have used be

相关标签:
1条回答
  • 2021-01-28 01:38

    Here, you can pass value in header with some variable name and define the same in mediation policies.

    The below Code calls different endpoints on the basis of variable "check" is present or not, if it is present in header with any value,the endpoint_B will get called, if value of check is not present or check is not present in header, then it will call endpoint_C.

    <sequence xmlns="http://ws.apache.org/ns/synapse" name="dynamic-endpoint-seq-boolean">
    <!--it checks the value for variabe check in header if there exist value for check 
    then it will call B, if value not exist or check is not present then C called-->
    <property name="uri.var.check" expression="get-property('transport','check')"/>
    <filter source="boolean(get-property('uri.var.check'))" regex="false">
    <then>
        <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
        <header name="To" expression="get-property('C')"/> 
    </then>
    <else>
        <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
        <header name="To" expression="get-property('B')"/>
    </else>
    </filter>
    </sequence>
    

    There is one more way to do the same thing, you can use Switch case in XML,as shown below and configure multiple endpoints. Here, if you pass the value of check as 'B' in header, endpoint_B will called, else if you pass value of 'check' as 'C' in header, then endpoint_C will get called, and if you pass any other value than 'B' or 'C' or not pass any value or even not pass check in header than default endpoint here endpoint_A will get called.

    <sequence name="dynamic_ep_switch" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <property name="uri.var.check" expression="get-property('transport','check')"/>
    <switch source="get-property('uri.var.check')">
        <case regex="B">
           <!-- We are then assigning the endpoint which we need to route to in a property named service_ep in this step -->
            <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
            <header name="To" expression="get-property('B')"/>
        </case>
        <case regex="C">
            <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
            <header name="To" expression="get-property('C')"/>
        </case>
        <default>
            <property name="A" expression="fn:concat('http://localhost:8080/Test/','getA')"/>
            <header name="To" expression="get-property('A')"/>
        </default>
    </switch>
    

    There is one more way to use switch if your endpoint are running on same host and port by using property="rest_url_postfix" as shown in below code. Here,output will be same as above but some changes that you need to make are for the above code you need to select dynamic endpoint in Endpoints tab in WSO2-APIM publisher while for the below code, you select REST Endpoints and select value of endpoint as http://<host_name>:<port_number>// . For example, http://localhost:8080/Test/

    <sequence name="dynamic_same_ep_switch" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <!-- The property which is retrieved as get-property('To')" stores the request URI for the API. Based on this value we will determine the endpoint which the request needs to be routed to.-->
    <property name="uri.var.check" expression="get-property('transport','check')"/>
    <switch source="get-property('uri.var.check')">
        <case regex="B">
           <!-- We are then assigning the endpoint which we need to route to in a property named service_ep in this step -->
            <property name="REST_URL_POSTFIX" value="getB" scope="axis2"/>
        </case>
        <case regex="C">
            <property name="REST_URL_POSTFIX" value="getC" scope="axis2"/>
        </case>
        <default>
            <property name="REST_URL_POSTFIX" value="getA" scope="axis2"/>
        </default>
    </switch>
    </sequence>
    

    For anymore information regarding sample mediation sequences refer following links:

    • https://docs.wso2.com/display/APICloud/Sample+Mediation+Sequences
    • https://docs.wso2.com/display/ESB480/HTTP+Transport+Properties

    Hope. This resolves your Query.

    0 讨论(0)
提交回复
热议问题