It is common to use query and path parameters when invoking integration services. Here we will be learning how we can read query and path parameters of the incoming request and access them inside the integration logic.
- Reading query params
There are two ways of doing this:
- Using synapse xpath variable $url
Using the synapse xpath variable is the easiest and the best way to access query parameters. It is faster than using the get-property function.
Let’s assume we are invoking an API service with two query params called ‘first_name’ and ‘last_name’. The following API service will read those query params and return them as the response payload.
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="ParamAPI" context="/params">
<resource methods="GET">
<inSequence>
<!-- set parameter values to response payload -->
<payloadFactory media-type="json">
<format>{"Hello":"$1 $2"}</format>
<args>
<arg evaluator="xml" expression="$url:first_name" />
<arg evaluator="xml" expression="$url:last_name" />
</args>
</payloadFactory>
<respond />
</inSequence>
</resource>
</api>
You can invoke the above APi using the URL:
https://localhost:8243/params?first_name=John&last_name=Doe
The above API service shows how to read query param values using $url:first_name, $url:last_name and set them in the response payload.
Similarly, you can read param values to properties as below.
<property name="first_name" expression="$url:first_name" scope="default" type="STRING" />
<property name="last_name" expression="$url:last_name" scope="default" type="STRING" />
- Using the get-property(‘query.param.<param_name>’)
Here is a sample API service that uses the get-property() function.
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="ParamAPI" context="/params">
<resource methods="GET">
<inSequence>
<payloadFactory media-type="json">
<format>{"HI":"$1 $2"}</format>
<args>
<arg evaluator="xml" expression="get-property('query.param.first_name')" />
<arg evaluator="xml" expression="get-property('query.param.last_name')" />
</args>
</payloadFactory>
<respond />
</inSequence>
</resource>
</api>
You will get a similar result as in earlier.
2. Reading path params
In order to read path params, we have to set the resource path dynamically. Ex: /{name}. Then we can read the param value using the get-property function as below.
get-property(‘uri.var.name’)
Try this API service to test the theory.
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="ParamAPI" context="/params">
<resource methods="GET" uri-template="/{name}">
<inSequence>
<payloadFactory media-type="json">
<format>{"HI":"$1"}</format>
<args>
<arg evaluator="xml" expression="get-property('uri.var.name')" />
</args>
</payloadFactory>
<respond />
</inSequence>
</resource>
</api>
Invoking the API: https://localhost:8243/params/John
The value ‘John’ set for the /{name} path parameter has been read and set to the response payload.
That’s it then. Now you know how to read query and path params in integration services and use them in the integration flow.
Thanks for reading.