Get URI VAR from SynapseXPath Variable in WSO2 ESB API - wso2

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.

Related

how to convert json to path variable data in wso2 esb?

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

how to pass query URL in wso2esb to endpoint

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.

How to retrieve HTTP REST METHOD of current service at Run time in WSO2 AM Sequence?

How to retrieve HTTP REST METHOD(GET,PUT,POST,DELETE,OPTIONS) in WSO2 Api Manager's Sequence at runtime? I tried to $ctx:REST_METHOD which returns 'null' value.
<sequence name="ec_rest_dynamic_ep" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property expression="$ctx:REST_METHOD" name="restmethod"
scope="default" type="STRING"/>
<log>
<property expression="get-property('restmethod')" name="*******************REST_METHOD***********"/>
</log>
</sequence>
Basically, HTTP REST METHOD value of current service & URL context of that service needed to identify the service in order redirect the service to its endpoint dynamically at runtime.
Try the following property.
<property name="Http_Method" expression="get-property('axis2', 'HTTP_METHOD')"/>
You can find more useful properties in [1].
#Pubci's answer is correct. Here is another way.
<property name="Method" scope="transport" expression="$ctx:api.ut.HTTP_METHOD"/>
Some other properties can be found here.

Is it possible to extract and/or pass along a query parameter in ESB REST proxy service

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&param2=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

Sending custom response from ESB

I have a string in a custom mediator in the inSequence . I want this string to be the final response from the esb . I want to sent this string back to the client as response.How can i do this?
After adding what ever the string you have inside the custom mediator you can send the current payload back to the client using the following config.
<header name="To" action="remove"/>
<property name="RESPONSE" value="true"/>
<send/>