How to add the same query parameter with different values multiple times using JavaScript ?

We have the following requirement wherein I want to set a specific query parameter "type" 3 times to the request object as shown in the example below:

"country=India&type=siteid:1&format=json&fullText=true&type=currencytypename:text&type=languagetypecode:promo" 

I used context.setVariable API to set the values for type 3 times as shown below:

var type = "siteid:1";
context.setVariable("request.queryparam.type", type);
type = "currencytypename:text";
context.setVariable("request.queryparam.type", type);
type = "languagetypecode:promo";
context.setVariable("request.queryparam.type", type);

However, when I check the request, I see only "type=siteid:1" exists but "type=authoringtypecode:promo" and "type=assettypename:text" are not in the request.

Can someone please let me know if there's a way to add the same query parameter with different values, multiple times using JavaScript ?

0 1 2,562
1 REPLY 1

yes, there's a way. Try embedding the following JavaScript in a policy in the request flow.

var qparams = {
      'country': 'India',
      'type' : [
        'siteid:1' , 'currencytypename:text', 'languagetypecode:promo'
      ],
      'format':'json',
      'fullText':'true'
    };

Object.keys(qparams).forEach(function(key) {
  var value = qparams[key];
  if (typeof value === 'string' ) {
    context.setVariable("request.queryparam." + key, value);
  }
  else {
    // we want to set multiple values
    value.forEach(function(item, ix){
      context.setVariable("request.queryparam." + key + '.' + (ix + 1), value[ix]);
    });
  }
});

You want to set the variables with names like this:

request.queryparam.QPARAMNAME.1
request.queryparam.QPARAMNAME.2
...

For the 1st, 2nd, and so on.