Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

HTTP Query Parameters on Demand

DZone's Guide to

HTTP Query Parameters on Demand

In this post, a software architect demonstrates how to add query parameters to HTTP calls, using the GET and PUT methods.

· Integration Zone ·
Free Resource

The new Gartner Critical Capabilities report explains how APIs and microservices enable digital leaders to deliver better B2B, open banking and mobile projects.

In this article, we will see how to add query params on demand. In the HTTP world, the HTTP GET method accepts query parameters as inputs and returns data based on those parameters. But sometimes, when the parameter's value is null, the parameter ends up passing nothing to the GET service. When your requirement is not to create parameters, it is important to know if the value is null.

Usually, HTTP query params can be configured statically in the below way, so that the value is null in the URL. It will be passed as empty, and, as a result, the provider service will return an unexpected result.

Image title

But what will happen if value2 is null? In this case, the URL will look like the below example:

http://myservername:port/myservicename?param1=value1&param2=&param3=value3&api_key=value4

For any HTTP call, we can use Postman so that the parameters can be edited in a table format as a key-value pair. In order to overcome this issue, we can add query.params as a HashMap(#[new java.util.HashMap()]) variable. Hashmap can store key-value pairs, and it can be used inside HTTP connectors.

Once we declare a variable, we can add an expression shape with the below expression:

if(message.inboundProperties.'http.query.params'.param1 != null)
flowVars.params.put("param1", message.inboundProperties.'http.query.params'.param1);

if(message.inboundProperties.'http.query.params'.param2 != null)
flowVars.params.put("param2", message.inboundProperties.'http.query.params'.param2);

if(message.inboundProperties.'http.query.params'.param3 != null)
flowVars.params.put("param3", message.inboundProperties.'http.query.params'.param3);

//if it is a mandatory
flowVars.params.put("api_key", flowVars.apikey);

Here, params is my flow's variable name and, using the put method, we can insert the query parameter. Based on the inbound property's value, we bet the value inserted into theparams  parameter.

The above expression adds param1param2, and param3 as conditionals, and api_key as mandatory.

Now, we can configure this variable in the HTTP connector as shown below.

Image title

Now we are done and the URL with the parameter will look like the below example if param2 is null:

http://myservername:port/myservicename?param1=value1&param3=value3&api_key=value4

The new Gartner Critical Capabilities for Full Lifecycle API Management report shows how CA Technologies helps digital leaders with their B2B, open banking, and mobile initiatives. Get your copy from CA Technologies.

Topics:
mule flow ,integration ,http get ,postman

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}