WSO2 ESB REST with dynamic parameters - wso2

I build up a API which should take dynamic parameters like below,
<api xmlns="http://ws.apache.org/ns/synapse" name="MrlDatabaseAPI" context="/rest">
<resource methods="OPTIONS GET" uri-template="/reportNotes?country={country}&pesticide={pesticide}&crop={crop}">
<inSequence>
<send>
<endpoint>
<http method="GET" uri-template="http://172.17.100.113/MRLService/rest/v1/reportNotes?country={uri.var.country}&pesticide={uri.var.pesticide}&crop={uri.var.crop}"/>
</endpoint>
</send>
</inSequence>
Now the resource only work if passing all three parameters, http://localhost/rest/reportNotes?country=AUS&pesticide=ABA3000&crop=22020100.
How can I tweak the API and make it accept any two parameters, like:
http://localhost/rest/reportNotes?country=AUS&pesticide=ABA3000 or http://localhost/rest/reportNotes?country=AUS&crop=22020100
The Restful web service itself can accept any number of parameters.
Thanks,
Sean

According to your configuration, it can't be solve with any number of parameters.
You may solve the issue with "filter" mediator
Ex:
<filter source="boolean(get-property('uri.var.pesticide'))" regex="false">
<then>
<send>
<endpoint>
<http method="GET" uri-template="http://172.17.100.113/MRLService/rest/v1/reportNotes?country={uri.var.country}&crop={uri.var.crop}"/>
</endpoint>
</send>
</then>
<else>
<drop/>
</else>
</filter>
You may implement your scenario with this mediator.

Related

WSO2: Avoid hard-coding URI ports in Address Endpoint

I have configured an offset of '10' to my ESB server on the carbon.xml file. Now all deployed API's are pointing to the port 8290.
In my API I am calling another API via an Http Endpoint. I am setting the URI template manually to: http://localhost:8290/paramAPI, as follows:
<send>
<endpoint>
<http method="get" uri-template="http://localhost:8290/paramAPI"/>
</endpoint>
</send>
However I also want to deploy to other server instances. For example, I would like to deploy to a server with an offset of '20', and therefore the '/paramAPI' would no longer be reachable at port '8290', instead on '8300'.
In this case, the Http endpoint inside the send mediator must become:
<send>
<endpoint>
<http method="get" uri-template="http://localhost:8300/paramAPI"/>
</endpoint>
</send>
I am searching for a method to parameterize the port value.
I have tried to use local entries by defining a string variable on the server by the name APIPort. When referencing it, I modified the HTTP endpoint as follows:
<send>
<endpoint>
<http method="get" uri-template="http://localhost:{APIPort}/paramAPI"/>
</endpoint>
</send>
but I got: java.net.MalformedURLException: For input string: "{APIPort}"
What is the best approach to parameterize URI's? if local entries are used then what is the proper way to use them as parameters and reference them in the URI?
Much appreciated and kind regards,
if I recall correctly you should use uri.var for your uri-template properties. Try to name your property uri.var.APIPort instead.
To be sure I created a simple API , see the following example:
<api xmlns="http://ws.apache.org/ns/synapse" name="testapi" context="/test" version="1" version-type="context">
<resource methods="POST GET">
<inSequence>
<property name="uri.var.version" value="v2"/>
<send>
<endpoint>
<http uri-template="http://mocky.io/{uri.var.version}/5d1dec2a3000006a00d7239d"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
</api>

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

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}.

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 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.