Friday, May 16, 2014

How to append a query parameter to an API published from WSO2 API Manager

How to append a query parameter to an API published from WSO2 API Manager

There are some situations where you need to send some url query parameters with the request when publishing an API through the API Manager. But You do not need client to send those parameters with the request URL. In that case how do you append those parameters to the endpoint url. In this blog post see how to append a query parametr to endpoint url.

First you need to create an API and published the API. Please refer how to create an API to create an API in WSO2 API Manager.

Then the requirement is once you invoke the API with some query parameter, It will call the actual service endpoint with some other query parameter.

You can append query parameters to the endpoint url from API Manager. synapse property 'REST_URL_POSTFIX' can be used to manage the query parameter. Once you create an api, what you have to do is that, you need to update the synapse configuration of the api created with the relevant property. the property REST_URL_POSTFIX will give you the query string coming into the api from the client. Then you can change the query string as you need.

<property name="REST_URL_POSTFIX" expression="fn:concat(get-property('axis2','REST_URL_POSTFIX'), '&amp;exchange=xyz')" scope="axis2" type="STRING"/>

Once You create an API, You can add parameter by following bellow steps.
1) Login to AM(https://ipaddress:port/carbon/) and Click on main > Source View
Then you can see the synapse configuration

2) Find the api configuration you have created by the api name.

3) Then update the insequence of the api with bellow property.
<property name="REST_URL_POSTFIX" expression="fn:concat(get-property('axis2','REST_URL_POSTFIX'), '&amp;exchange=xyz')" scope="axis2" type="STRING"/>

4) Click on update button.

Then your api looks as bellow. 

 <api xmlns="http://ws.apache.org/ns/synapse" name="admin--testapi" context="/testapi" version="1.0.0" version-type="url">  
   <resource methods="GET" url-mapping="/*">  
     <inSequence>  
       <property name="POST_TO_URI" value="true" scope="axis2"/>  
       <property name="REST_URL_POSTFIX" expression="fn:concat(get-property('axis2','REST_URL_POSTFIX'), '&amp;exchange=xyz')" scope="axis2" type="STRING"/>  
       <filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">  
         <then>  
           <send>  
             <endpoint name="admin--testapi_APIEndpoint_0">  
               <address uri="http://localhost:8080/stockquote/getQuote">  
                 <timeout>  
                   <duration>30000</duration>  
                   <responseAction>fault</responseAction>  
                 </timeout>  
                 <suspendOnFailure>  
                   <errorCodes>-1</errorCodes>  
                   <initialDuration>0</initialDuration>  
                   <progressionFactor>1.0</progressionFactor>  
                   <maximumDuration>0</maximumDuration>  
                 </suspendOnFailure>  
                 <markForSuspension>  
                   <errorCodes>-1</errorCodes>  
                 </markForSuspension>  
               </address>  
             </endpoint>  
           </send>  
         </then>  
         <else>  
           <sequence key="_sandbox_key_error_"/>  
         </else>  
       </filter>  
     </inSequence>  
     <outSequence>  
       <send/>  
     </outSequence>  
   </resource>  
   <handlers>  
     <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler"/>  
     <handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleHandler">  
       <property name="id" value="A"/>  
       <property name="policyKey" value="gov:/apimgt/applicationdata/tiers.xml"/>  
     </handler>  
     <handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageHandler"/>  
     <handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtGoogleAnalyticsTrackingHandler"/>  
     <handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>  
   </handlers>  
 </api>


Ex: Client request  http://localhost:8280/testapi/1.0.0?symbol=abc
Actual request required for back end
http://localhost:8080/stockquote/getQuote?symbol=abc&exchange=xyz

Here you have added exchange parameter to the service call. As above you can modify the query string as you required.