How to add query params to URL in API in WSO2 ESB - wso2

I need to append query params to endpoint URI according to query params in request.
I have an resource API in ESB published like this:
<resource methods="GET" url-mapping="/searchEngine/sortAndFilterVolunteers*">
<inSequence>
<send>
<endpoint>
<address
uri="http://localhost:8080/project-web-services/services/project-rs"></address>
</endpoint>
</send>
</inSequence>
I have to append query params dinamically.
How could I do that?

Following sample API definition may help you.
<api xmlns="http://ws.apache.org/ns/synapse" name="sample" context="/api/sample">
<resource methods="OPTIONS GET" uri-template="/{val1}/groups/{val2}.json?q1={v1}&q2={v2}">
<inSequence>
<property name="uri.var.q1" expression="$url:q1"></property>
<property name="uri.var.q2" expression="$url:q2"></property>
<property name="uri.var.val1" expression="get-property('uri.var.val1')"></property>
<property name="uri.var.val2" expression="get-property('uri.var.val2')"></property>
<send>
<endpoint>
<http method="GET" uri-template=""></http>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>

Add the query parameter to uri-template in resource as below :
<resource methods="GET" uri-template="/getStudents/{id}">
When sending to the endpoint,
<send>
<endpoint>
<http method="get" uri-template="http://studentsapi/student/{uri.var.id}"/>
</endpoint>
</send>

Add the query parameter to url-mapping in resource as below : url-mapping="/*"
*
you can send a request may be server:port/service?q1={q1} and other request may be server:port/service?q2={q2}&q3={q3}.

Related

WSO2 passing a value from request

I'm new in WS02 but I'm having a trouble to get a url param and pass it.
Following the Sending a Simple Message example
my resource is
<resource methods="GET" uri-template="/wholesales/{dlrcode}">
<inSequence>
<log level="custom">
<property name="message" value="Whole sales request"/>
<property expression="/default/expression" name="p_dlrCode"/>
</log>
<send>
<endpoint key="WholeSales"/>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence/>
</resource>
and my END is
<?xml version="1.0" encoding="UTF-8"?>
<endpoint name="WholeSales" xmlns="http://ws.apache.org/ns/synapse">
<http method="get" uri-template="http://xxx/RDRSvc//vehicle/wholesaleD/{url.var.dlrcode}?format=json"/>
</endpoint>
but stil when I call api like
http://xxx.xx.xx.xx:8280/rdr/wholesales/42103
I see no dlrCode ???
TID[-1234] [ESB] [2017-03-07 11:08:29,464] INFO {org.apache.synapse.mediators.builtin.LogMediator} - message = "Whole sales request", p_dlrCode =
how can I put the 42103 in property p_dlrCode?
Try this:
Your resource:
<resource methods="GET" uri-template="/wholesales/{dlrcode}">
<inSequence>
<log level="custom">
<property name="message" value="Whole sales request"/>
<property expression="get-property('uri.var.dlrcode')" name="p_dlrCode"/>
</log>
<send>
<endpoint key="WholeSales"/>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence/>
</resource>
Your endpoint fixed:
<?xml version="1.0" encoding="UTF-8"?>
<endpoint name="WholeSales" xmlns="http://ws.apache.org/ns/synapse">
<http method="get" uri-template="http://xxx/RDRSvc//vehicle/wholesaleD/{uri.var.dlrcode}?format=json"/>
</endpoint>

How to extract http headers using a mediator

How do I extract http headers like
Authorization: ​"admin 0PN5J17HBGZHT7JJ3X82"
where admin is the username and 0PN5J17HBGZHT7JJ3X82 is the password and assign it to a property/variable which would be then passed to a dss service for user login verification. From what I know our API can be do this using custom sequences and mediators (https://docs.wso2.com/display/AM170/Adding+Mediation+Extensions) but its not clear tome on how to extract this header and assign it to different property names like login and password.
Does a mediator header can take care of this? Or this there another way of doing by using a proxy service?
Header Mediator
<in>
<header name="Authorization" value="admin 0PN5J17HBGZHT7JJ3X82" scope="transport"/>
<send>
<endpoint name="people">
<address uri="http://localhost:9443/testapi/" format="get"/>
</endpoint>
</send>
</in>
<out>
<send/>
</out>
Proxy Service
<proxy name="adminServiceProxy" transports="https http"
startOnLoad="true" trace="disable">
<description/>
<target>
<endpoint>
<address uri="https://localhost:9443/testapi"/>
</endpoint>
<inSequence>
<property name="Authorization"
expression="fn:concat('Basic ','admin:0PN5J17HBGZHT7JJ3X82')"
scope="transport"/>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>
Thank you
You can extract like this;
<property name="AuthHeader" expression="$trp:Authorization"/>
Then log it and see what you are retrieving..
<log>
<property name =" Authheder value" expression=get-property('AuthHeader')/>
</log>
Then construct Basic auth header as you pointed in your proxy configuration.
Here is a blog post which explains how you can retrive various information from a sequence

How to read query param in WSO2 ESB 4.8 or above?

I have a rest end point in WSO2ESB (4.8),I need to read query parameter to set as dynamic payload as the my business ,But i failed to read it due to newer with wso2 ESB.Any help ?
The bellow code may help you
<api xmlns="http://ws.apache.org/ns/synapse" name="sample" context="/api/sample">
<resource methods="OPTIONS GET" uri-template="/{val1}/groups/{val2}.json?q1={v1}&q2={v2}">
<inSequence>
<property name="uri.var.q1" expression="$url:q1"></property>
<property name="uri.var.q2" expression="$url:q2"></property>
<property name="uri.var.val1" expression="get-property('uri.var.val1')"></property>
<property name="uri.var.val2" expression="get-property('uri.var.val2')"></property>
<send>
<endpoint>
<http method="GET" uri-template=""></http>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>
Define a REST API inside ESB and access to query params with get-property('query.param.xxx') or get-property('uri.var.yyy'), sample :
<resource methods="GET" uri-template="/testwso2/{symbol}?arg1={value1}">
get-property('query.param.arg1')
get-property('uri.var.symbol')

Adding parameters to the endpoint url wso2 esb

I am new to the WSO2 esb. I want to create a api in which i want to add a dynamic parameter from api Url to endpoint url.
My end point url is like http://example.com/api/sync/{session_id}.json
I tried to create api like
<api name="rest" context="/sync">
<resource methods="GET" uri-template="/{id}">
<inSequence>
<log level="full">
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="get-property('uri.var.id')"
scope="axis2"/>
</log>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<send>
<endpoint name="Mecars">
<http trace="enable"
method="get"
uri-template="http://example.com/sync/{+uri.var.session}.json"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
In log i get the value of uri.var.session But on the endpoint Uri-template it is not appending.
Please guid me how append the {id} value in the api uri-template to end point uri-template?
Check this sample
I recommend you to do string concatenation at property mediator adn use that directly at uri-template, rather adding "variable +.json" at uri-template.
That is;
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="get-property('uri.var.id')"
scope="axis2"/>
In the above for expression do string concatenation to construct full variable with ".json" ending.
Please remove the '+' symbol from your http end point.Its working fine to me
sample API
<api xmlns="http://ws.apache.org/ns/synapse" name="Elastic Search Using NPI" context="/api/npi/professional/npi.json">
<resource methods="OPTIONS GET" uri-template="/{npi}">
<inSequence>
<log level="full">
<property name="uri.var.npi" expression="get-property('uri.var.npi')"></property>
</log>
<send>
<endpoint>
<http method="get" uri-template="example.com/{uri.var.npi}"></http>
</endpoint>
</send>
</inSequence>
</resource>
</api>
Change the http endpoint uri template like follows.
<http trace="enable" method="get" uri-template="http://example.com/sync/{uri.var.id}.json"></http>
Following is the modified api configuration.
<api xmlns="http://ws.apache.org/ns/synapse" name="rest" context="/sync">
<resource methods="GET" uri-template="/{id}">
<inSequence>
<log level="full">
<property xmlns:ns="http://org.apache.synapse/xsd" name="uri.var.session" expression="get-property('uri.var.id')"></property>
</log>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"></property>
<send>
<endpoint name="Mecars">
<http trace="enable" method="get" uri-template="http://example.com/sync/{uri.var.id}.json"></http>
</endpoint>
</send>
</inSequence>
</resource>
</api>
You can do the string concatenation at the property mediator as follows.
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="fn:concat(get-property('uri.var.id'),'.json')"
scope="axis2"/>

adding post request in my WSO2 proxy service

I have created a proxy service who transform a message using a xslt mediator and then transformed to JSON,
I want now to post the JSON message in a rest web service how i can do that directely in my proxy service?
This is my proxy service :
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CategoryProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="Authorization" expression="fn:concat('Basic ', base64Encode('admin:admin'))" scope="transport" type="STRING"/>
<send>
<endpoint>
<address uri="http://localhost:8068/database/library.author/301"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<xslt key="conf:/ressources/catXsl.xsl"/>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<send/>
</outSequence>
<faultSequence/>
</target>
<description></description>
</proxy>
I want the message sent by this proxy to be post in a rest web ressource, How I can do it in my proxy?
Looks like you are looking for service chaining and you can do that in two ways:
1) Using a receiving sequence
<inSequence>
<property name="Authorization" expression="fn:concat('Basic ', base64Encode('admin:admin'))" scope="transport" type="STRING"/>
<send receive="receivingSeqForChaining">
<endpoint>
<address uri="http://localhost:8068/database/library.author/301"/>
</endpoint>
</send>
</inSequence>
You can define whatever sequence of operations you want in that receiving sequence. Output from this will be handed out to receiving sequence. If you don't want to do any specific action in outSequence for this case you can just add a to that.
2) Using outSequence to trigger other service call
You can also make that http call directly from outSequence something like this:
<outSequence>
<send>
<endpoint>
<http method="get"
uri-template="https://www.abc.com/resource/foo"/>
</endpoint>
</send>
</outSequence>
Hope this helps.