javascriptpolicy with multiple queryparams

Not applicable

Hi ,

I am working on Javascript policy,in that javascript needs to process multiple queryparameters,split them into single variables and send as a response.I have created a javascript policy and added to proxy post flow(proxy with no endpoint)

I have written below code:

var queryParamNames = context.getVariable("request.querystring");

var a = queryParamNames.split("&");

var length = a.length;

context.setVariable('test_qparam_names_length', length);

var i = 0;

a.forEach(function(item) { //context.proxyResponse.body.asXML;

context.setVariable('test_qparam_name_found' + i++, true);

// set vehicleord = context.setVariable('test_qparam_name_' + i++, item.toString());

print("vehicle order ",context.setVariable('test_qparam_name_' + i++, item.toString()));

});

I am trying to show all split values as proxy response.

Here is my url:http://proxybasepath/vehicleId?VIN=11&VIN=22&VIN=33

I want to display 11,22,33 as proxy response.I tried using variable 'proxyResponse' ,getting error.

could you please suggest me this.

0 1 275
1 REPLY 1

Hi, this is a basic JavaScript question, "how do I parse through a string with JavaScript?"

There are lots of options. Here's one that might work for you.

var queryParamNames = context.getVariable("request.querystring");
var a = queryParamNames.split("&");
context.setVariable('v_qparam_names_length', a.length.toFixed(0));
var vinValues = [];
a.forEach(function(item, ix) {
  item = item + '';
  context.setVariable('v_qparam_item_found_' + ix, true);
  context.setVariable('v_qparam_item_' + ix, item);
  var pair = item.split('=');
  if (pair.length>0) {
    context.setVariable('v_qparam_item_' + ix +'_key', pair[0]);
    if (pair.length>1) {
      context.setVariable('v_qparam_item_' + ix +'_value', pair[1]);
      if (pair[0] == 'VIN') {
        vinValues.push(pair[1]);
      }
    }
  }
});
context.setVariable('v_values', vinValues.join(',')); // eg 11,22,33

And then you would use AssignMessage to insert the value string into the place you want it.

If you want to set the response content from JavaScript, then you would do this:

context.setVariable('response.content', vinValues.join(','));

...But then you also need to set the content-type header. And maybe the status code, and other headers... AssignMessage might be a better approach to accomplish those purposes.

<AssignMessage name='AM-Response'>
  <Set>
    <Headers><Header name='Dino-was-here'>value-here</Header></Headers>
    <Payload contentType='text/plain'>{v_values}</Payload>
    <StatusCode>200</StatusCode>
    <ReasonPhrase>OK</ReasonPhrase>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew='false' transport='http' type='response'/>
</AssignMessage>

Just configure that policy to run after the JS that shreds the qparams.