JavaScript policy to pull out query or form parameter disregarding case

nickolsen
Participant I

In reference to this post: https://community.apigee.com/questions/65593/verify-api-key-query-parameter-or-form-parameter.html, is there an example using javascript that would pull out the apikey value from either the query parameters or form parameters ignoring case sensitivity?

0 7 2,457
7 REPLIES 7

sidd-harth
Participant V

For query parameter you can use this JS,

var token = "x-api-key";
var tokenValue ;


var queryParamsList = request.queryParams;
for(var queryParam in queryParamsList){
    // convert to lower case and check if the query parameter is token
    if (queryParam.toLowerCase() == token) {
        // Get the value of the query parameter
        tokenValue = context.getVariable("request.queryparam." + queryParam);
        // Remove the query parameter
        context.removeVariable("request.queryparam." + queryParam);
    }
}
// Set the value
context.setVariable("apigee.apikey", tokenValue);

Try and come up with a similar logic for Formparams as well.

Great, this is helpful. For form parameters, is there a request.formParams property just like there is request.queryParams?

Yes, if you see in the previous example I have used it in many places,

request.formparam.xxxxxxxxxxxx

Forgive me, I did see those and I can see that if I make a call to context.getVariable("request.formparam.xxxxx") I can retrieve a given value for a specific form parameter as long as I get the casing correct. But, we can't seem to figure out how to iterate over the names/keys of the form parameters. We tried:

request.formparam
request.formParams
request.formparams
request.formparam.names (from docs online)

All of the above return null or undefined even when we can extract specific form parameter values out if we know the exact name of the parameter.

Can you shed some light on how to get a list of all form parameter names so we could write a loop around context.getVariable("request.formparam." + formParam)

Can you double-check? The docs say to use request.formparams.names .

not request.formparam.names

So we tried context.getVariable("request.formparams.names") and that returns a collection object which has a bunch of methods on it. We tried calling the toArray() method on it assuming it would return an array of names but when using the following code

var formParamsList = context.getVariable("request.formparams.names").toArray();
for(var formParam in formParamsList){
        print('Form Param=' + formParam);
}

It prints out:

Form Param=0
Form Param=1
Form Param=2
Form Param=3

The "names" seem to just return 0, 1, 2, and 3.

As an added note, just trying to do the following:

var formParamsList = request.formparams.names;

throw a null reference exception. formparams returns null

Nick, I'm sorry it's been such a hassle. The little details can be maddening.

I can build a proxy that echos back whatever formparams are passed in, like this:

curl -i https://$ORG-$ENV.apigee.net/formparam-handler/t1 -X POST -d 'arg1=7&q=kjsdckjd&t=0.234' 
HTTP/1.1 200 OK
Date: Fri, 15 Feb 2019 19:32:20 GMT
Content-Type: text/plain
Content-Length: 59
Connection: keep-alive


formparam[arg1]=7
formparam[q]=kjsdckjd
formparam[t]=0.234

The key is this JS policy:

<Javascript name='JS-ProcessFormParams' timeLimit='200' >
  <Source>
    var namesString = context.getVariable("request.formparams.names")+'';
    var names = namesString.slice(1,-1).split(',');
    print(JSON.stringify(names)); // will appear in Trace UI
    var responseTxt = ''
    names.forEach(function(name) {
      name = name.trim();
      var thisParam = 'formparam['+ name+']=' +
        context.getVariable("request.formparam." + name);
      print(thisParam);
      responseTxt += thisParam + '\n';
    });
    context.setVariable('response.content', responseTxt);
    context.setVariable('response.header.content-type', 'text/plain');
  </Source>
</Javascript>

Obviously you don't need the "print" statements - those are only for the Trace UI.