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'), '&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'), '&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'), '&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.
I was looking for this solution and no where I found the straight solution. This one worked for me. Thanks :).
ReplyDeleteHowever, I gave a try to URLRewrite medaitor as well but without succes. :(
Hi I worked on this,But I didn't get the results.I'm using 1.9.0 version of WSO2 APIM. In this version I can test the published API in API console.While testing I didn't get the "exchange=xyz" in the requested URL.Please help me on this asap.
ReplyDeleteexchange parameter is appended to backend url. It will not comes to API console.
Deleteok...can we see the parameter and its value in the Url or in cURL??which was hidden.Because I'm not getting any response from server.I wanted to know whether this hidden parameter is passed or not.
DeleteYou can see the incoming and outgoing http messages on API Manager by enabling a wire logs in server.
Delete1) Open log4j.properties in $AM_HOME/repository/conf/
2) Find the following line and uncomment. Then save the log4j.properties file.
log4j.logger.org.apache.synapse.transport.http.wire=DEBUG
3) Restart the Server
Then once you send the messages to the API Manager, You can see the http message details on console
This comment has been removed by the author.
ReplyDeleteHi Nuwan,
ReplyDeleteWe are trying to use REST_URL_POST fix when the content type is text/xml and HTTP method is POST.
But it doesn't seems to be working for this particular combination.
It works when content type is application/xml but not when it is text/xml.