Search code examples
wso2wso2-api-manager

How to check whether query parameter is added to the request path


I have an api that some times have query parameter and sometime not. I want to add a query parameter to to the request path and can i do it with message mediation.

If there aren't query parameter, the parameter need to be added with "url?parameter=1" and if there is query parameter, parameter need to be added with "url?oldparameter=1&parameter=1".

I am trying with this example but this is for ei and I want for apimanager 260.

I checked the question but this does add "?" directly and I need to check whether query param is there and add "?" or "&".


Solution

  • If you are using APIM 4.1.0, we have a policy specifically created for this functionality (The policy Add Query Param). Let me share the policy logic in here and you can use it with mediation policy with APIM 2.6.0.

    <property name="rest_postfix" expression="get-property('axis2', 'REST_URL_POSTFIX')"/>
    <filter regex=".*\?.*" source="get-property('rest_postfix')">
            <!-- if there are query params already defined -->
            <then>
                <property name="REST_URL_POSTFIX" expression="fn:concat(get-property('rest_postfix'), '&amp;{{paramKey}}={{paramValue}}')" scope="axis2" type="STRING"/>
            </then>
    
            <!-- if there are no query params defined -->
            <else>
                <property name="REST_URL_POSTFIX" expression="fn:concat(get-property('rest_postfix'), '?{{paramKey}}={{paramValue}}')" scope="axis2" type="STRING"/>
            </else>
    </filter>
    

    You just need to replace the param key and param value with the required static content.