Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How do you iterate Collection from "request.queryparams.names" in JS ?

Not applicable

I am creating a JavaScript policy for an API and I am attempting to iterate the query parameter names to see which ones have been sent.

I used the context variable "request.queryparams.names" which I saw in the documentation as returning a variable of type "Collection"

var queryParamNames = context.getVariable("request.queryparams.names");

I could not iterate this variable in a loop nor could I invoke a "hasOwnProperty()" method on it. It kind of does not make sense to me as a Collection in JavaScript either is an array or a structured object and this seems to be neither.

When I trace my api request and see the variable in listed it shows up like [value1, value2] which looks like an array, but if I invoke

Object.prototype.toString.call(queryParamNames)

The type comes out to the JavaObject.

So firstly, why is that ?

Secondly, is this what is meant by a Collection because the docs don't say you get back a JavaObject ?

Thirdly, how do I iterate the query parameter names of this JavaObject ?

Thanks.

Solved Solved
3 14 10.8K
1 ACCEPTED SOLUTION

Hi @Georges Haddad

Sorry you're having trouble using the request.queryparams.names context variable from within JS.

I cannot solve that immediate problem for you, but I can suggest something that might be useful as a workaround. I use URI.js often within my JS for parsing query params and so on.

To use it, you'd need to include the source of URI.js into your resources/jsc directory, and you'd need to specify it like this in the JS callout policy configuration:

<Javascript name='Javascript-ParseQparams' timeLimit='200' >
  <IncludeURL>jsc://URI.js</IncludeURL> 
  <ResourceURL>jsc://parseQparams.js</ResourceURL>
</Javascript>

And then your JS code would look something like this:

var uri = URI(context.getVariable('request.uri'));

// get data map:
var search = uri.search(true);

// suppose the inbound API request looks like:
// http://host/basepath/suffix?param1=value1a&param1=value1b&param2=value2
//
// then, the search object would look like this: 
// {
//   "param1": [
//     "value1a",
//     "value1b"
//   ],
//   "param2": "value2"
// }

View solution in original post

14 REPLIES 14