Making query params optional in IBM API Connect

3.9k views Asked by At

Hi we are using IBM Api Connect as a gateway for our api's, i am unable to figure out how to make query params optional, i tried doing it in the DESIGN and ASSEMBLE sections of IBM api connect, but no luck.

this is my final URL that i want to invoke www.testdomain.products/getProducts?param1 = " "& param2 = " " & param3 = " "

here all the params are optional i am giving this url as

www.testdomain.products/getProducts?param1=$(request.parameters.param1)&param2=$(request.parameters.param2) & param3=$(request.parameters.param3)

in one use case i am only passing param1, and want the final url to be constructed only with param1 but this is what i am seeing

www.testdomain.products/getProducts?param1="value"&param2=&param3=

what should i do so that the url is constructed with only the values that i am passing, like this

www.testdomain.products/getProducts?param1="value"

2

There are 2 answers

5
Srikanth Pragallapati On

This can be achieved by creating the dynamic target URL using a script rather than setting static backend service URLin invoke/Proxy action.

Assembly section

Get GatewayScript Action before invoke

var targetUrl = "www.testdomain.products/getProducts?";
var input_param1 = apim.getvariable('request.parameters.param1');
var input_param2 = apim.getvariable('request.parameters.param2');


if(input_param1 !==null || input_param1 !="" || input_param1 != undefined ){
    targetUrl = targetUrl.concat("param1="+input_param1))
}
if(input_param2 !==null || input_param2 !="" || input_param2 != undefined ){
    if(targetUrl.indexOf('param1') > 0){
        targetUrl = targetUrl.concat('&')
    }
    targetUrl = targetUrl.concat("param2="+input_param2))
}

apim.setvariable('target-url',targetUrl)

In Invoke/Proxy - Use this $(target-url)

There might be some syntax issue, but we can achieve using the above way. Thanks!

0
Brendon S On

It looks like you just want to proxy the query string? In which case you can use $(target-url)$(request.search) as your invoke URL.