I have an Endpoint : http://localhost:8080/customer/get/1
and i want to send a json data to api in wso2esb and transform the value of id to path variable and send it to the Endpoint.
something like this:
{"id":"1"}
how can i do this?
please give me some advice.
You should build Uri-template path, with property named with prefix uri.var. For example named: uri.var.myId. and extract from JSON interesting value.
Next you can use that property in http endpoint in uri-template in brackets.
For your example, what you are looking for is something like this:
<property name="uri.var.myId" expression="json-eval($.id)" scope="default" type="STRING"/>
<call>
<endpoint>
<http uri-template="http://localhost:8080/customer/get/{uri.var.myId}"/>
</endpoint>
</call>
It is more precise described in wso2esb 4.9.0 documentation
Related
I am using wso2 API manager 3.2 and in my API there are some headers and Query Parameter.
My API has some path variable in URL in this way
http://example.com/data/readsomeData/{entityId}/{someId}
for the path variable I use the following mediator
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
and it works fine.
also I change some of incoming header's name . For example incoming header is IN_HEADER , I translate it to OUT_HEADER ,my backend expects OUT_HEADER.
I also use this mediator
<property name="IN_HEADER" expression="get-property('transport', 'IN_HEADER')"/>
<property name="OUT_HEADER" expression="get-property('IN_HEADER')" scope="transport"/>
<property name="IN_HEADER" scope="transport" action="remove"/>
It also works fine too.
The given API describe above also has some optional query parameters (limit, max, min) . For example if I use limit=10 in my API, I have to get 10 records, with out limit I get just one record.
In WSO2 API Manager Publisher I defined the above query parameters as the other parameters.
The problem is when I use each of query parameter I get the result same as the way I do not use the query parameters. I get only one record.
I think the API manager does not pass the query parameters to backend.
I don't know this problem related to the mediator I use or not!
You can approach this in a few different ways, below examples should also work on 3.2.0.
Because you remove the REST_URL_POSTFIX the APIM also removes the query parameters.
So option 1:
You could do something similar to what you did for the other headers.
Grab the query parameters and add them as headers for further use. (the $url shorthand should also be available in 3.2.0 as far as I know - it grabs a query parameter by name.)
<property name="limit" expression="$url:limit" scope="transport"/>
<property name="min" expression="$url:min" scope="transport"/>
<property name="max" expression="$url:max" scope="transport"/>
Or you could grab the query parameters from your URL and put them back after removing the POSTFIX.
<property name="newUrlPostfix" expression="substring after($axis2:REST_URL_POSTFIX, '?')"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<property name="REST_URL_POSTFIX" scope="axis2" expression="concat('?', $ctx:newUrlPostfix"/>
I'm pretty new on WSO2 IE and I'm trying to use a SOAP request to call a REST API.
This first part is OK but one of my API's needs to receive an attribute into its PATH or QUERY parameters. The attribute is sent by the soap call in it's body.
The question is, how can i get this body attribute and pass it in the PATH/QUERY param of the URL dynamically before i send it?
Picture of my architecture
Your requirement is to construct a dynamic URL. This can be easily done with the ESB Server. Here we have different options to construct this dynamic URL. Following is a sample proxy service that was created to achieve this.
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="sample_proxy"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log level="custom">
<property expression="$body//target" name="Target endpoint"/>
</log>
<property name="uri.var.host" value="http://localhost:8280"/>
<property name="uri.var.context" value="services"/>
<property expression="$body//target" name="uri.var.resourcepath"/>
<call>
<endpoint>
<http method="GET"
uri-template="{uri.var.host}/{uri.var.context}/{uri.var.resourcepath}"/>
</endpoint>
</call>
<respond/>
</inSequence>
</target>
<description/>
</proxy>
We are sending the following request to this proxy service.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<target>sample_path</target>
</soapenv:Body>
</soapenv:Envelope>
In the above proxy service what we have done is construct the dynamic URL with the parameters from the soap message please refer to the blog post [1] to further clarify regarding dynamic URLs.
Here in order to extract the parameters from the body, we have used XPaths. Please refer to the documentation [2] to further clarify regarding XPaths. We have extracted the value inside the target element and added it to the end of the URL. Thus this will invoke the http://localhost:8280/services/sample_path from the ESB server.
If you are interested in constructing the URL with query parameters you can do that by concatenating the required URL and using an address endpoint [1].
[1]-https://dzone.com/articles/constructing-dynamic-endpoint-urls-in-wso2-esb
[2]-https://docs.wso2.com/display/EI6xx/Accessing+Properties+with+XPath#AccessingPropertieswithXPath-$ctx
I'm having the following API configured in WSO2ESB:
<api xmlns="http://ws.apache.org/ns/synapse" name="service" context="/service">
<resource methods="POST">
<inSequence>
<call>
<endpoint>
<http method="POST" uri-template="https://webapps.localhost/service.php"/>
</endpoint>
</call>
<send/>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence>
<log level="full"/>
</faultSequence>
</resource>
</api>
The call works fine and POST content goes fine to the endpoint.
curl -X POST -d "a=1&b=2" localhost:8280/service
from the service.php file I can extract POST parameters fine.
Now if I want to have dynamic GET parameters passed as is to the endpoint, what would be the way to do it?
curl -X POST -d "a=1&b=2" localhost:8280/service?c=3&d=4
I know (at least what i understood) property mediator could be used but this is for known parameters in the query url (for example, $url:c) but I don't want to limit it, just pass the query url as is to the destination endpoint.
Any help would be appreciated.
You can access the resource path through REST_URL_POSTFIX
<property name="path" expression="$axis2:REST_URL_POSTFIX"/>
According to your request URL, $ctx:path should contain ?c=3&d=4
Just to update here, use Address EndPoint instead of HTTP EndPoint (which is based on URI-Template)
Usually what I do is defining two variables on the endpoint's URI template, and then use the property mediators to set them.
Something like
<http method="POST" uri-template="https://webapps.localhost/service?c={uri.var.c}&d={uri.var.d}"
Then use the property mediator to set the property uri.var.c and uri.var.d with the intended values.
I want to get a URI VAR value from request to call correct case in switch in WSO2 ESB API resource, like this:
<api name="apk" context="/apk"><resource methods="GET" uri-
template="/apk/{appName}"><inSequence><header name="App"
scope="transport" action="remove"/><switch source="get-
property('uri.var.appName')"><case regex="BEBE"><send><endpoint><http
method="GET" uri-template="http://localhost/apk/Bebe.apk></http>
</endpoint></send></case><case regex="CITAS"><send><endpoint><http
method="GET" uri-template="http://localhost/apk/Citas.apk></http>
</endpoint></send></case></switch></inSequence></resource></api>
In switch, source="get-property('uri.var.appName')" it's not correct.
Can I get this value using $url SynapseXpath valiables like $url?
What you have done is correct. Please try the same with a log mediator and see whether you get the value properly.
<log level="custom">
<property name="AppName" expression="get-property('uri.var.appName')"/>
</log>
Alternatively you can assign this to a property and use that inside your switch mediator.
I am building a ReST to ReST proxy service. I need to be able to pass along some query parameters to that service that are incoming with the request. E.g.
myhost.zz/proxyService?foo=1&bar=2
When I define such proxy - and later try to extract the value of 'foo' I get null.
So is it possible to achieve?
You shouls define an API (if you really want a proxy service, look at the end of this answer) :
<api xmlns="http://ws.apache.org/ns/synapse" name="proxyService" context="/proxyService">
<resource methods="POST GET OPTIONS DELETE PUT">
<inSequence>
<property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" type="STRING"></property>
<log level="custom">
<property name="foo" expression="get-property('query.param.foo')"></property>
<property name="bar" expression="get-property('query.param.bar')"></property>
</log>
</inSequence>
</resource>
</api>
call it with this url : http://host:port/proxyService?foo=12&bar=14
look at wso2-esb-service.log : INFO __SynapseService foo = 12, bar = 14
in the "resource", you can define a uri-template (URL Style = uri-template) with, for exemple, "/{scope}/*" and then when you call your api with http://host:port/proxyService/toto?foo=12&bar=14, you can access the "scope" with get-property('uri.var.scope')
to send a REST request, use a http endpoint with a uri-template using the same logic : uri-template="http://other_host:port/Service/{uri.var.scope}/truc?abc={query.param.foo}&dfc={query.param.bar}"
-->
If you want to use a proxy service, you can access to query parameters like this :
request : http://esb:8280/services/MonService?param1=val1¶m2=val2
<property name="PARAM1"
expression="tokenize(substring-after(syn:get-property('To'),'param1='),'&')"
scope="default"
type="STRING"/>
<property name="PARAM2"
expression="tokenize(substring-after(syn:get-property('To'),'param2='),'&')"
scope="default"
type="STRING"/>
you can use synapse xpath variable $url to read the query parameter values.
check this[1] for example.
Reading Dynamic query parameter in WSO2 APIM