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

**URL:** <https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968>\
**Category:** Apigee\
**Tags:** api-runtime\
**Created:** [November 10, 2015, 5:45am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968 "2015-11-10T05:45:43Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 10, 2015, 5:45am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/1 "2015-11-10T05:45:43Z")

</div>

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”

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![anilsr](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@anilsr](https://discuss.google.dev/u/anilsr)\
**Post date:** [November 10, 2015, 6:14am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/2 "2015-11-10T06:14:17Z")

</div>

Dear [@Georges Haddad](https://community.apigee.com/users/2968/georgeshaddad.html) , Welcome to Apigee Community. We see the same results. Let us look into it.

---

<div class="post-metadata">

**Author:** ![anilsr](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@anilsr](https://discuss.google.dev/u/anilsr)\
**Post date:** [November 10, 2015, 4:31pm UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/3 "2015-11-10T16:31:04Z")

</div>

[@krupa](https://community.apigee.com/users/1180/krupa.html) , Any idea what is the root cause ?

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 10, 2015, 8:12pm UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/4 "2015-11-10T20:12:27Z")

</div>

Hi [@Georges Haddad](https://community.apigee.com/users/2968/georgeshaddad.html)

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](http://medialize.github.io/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:

```auto
<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:

```auto
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](http://host/basepath/suffix?param1=value1a&param1=value1b&param2=value2)
//
// then, the search object would look like this: 
// {
// "param1": [
// "value1a",
// "value1b"
// ],
// "param2": "value2"
// }

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 11, 2015, 4:48am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/5 "2015-11-11T04:48:08Z")

</div>

Thanks [@Dino](https://community.apigee.com/users/803/dchiesa.html), that’s a good way to go around the issue. I actually did the same workaround but manually parsing the querystring. This URI.js package looks like it can do the job better.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 12, 2015, 5:08am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/6 "2015-11-12T05:08:08Z")

</div>

Hi [@Georges Haddad](https://community.apigee.com/users/2968/georgeshaddad.html)

Hope this help!

//Convert Collection to string

var queryFieldsCollection = context.getVariable(‘request.queryparams.names’) + ‘’;

//Remove square brackets

queryFieldsCollection = queryFieldsCollection.substr(1, queryFieldsCollection.length - 2);

//Split string into an array

var queryFieldsArray = queryFieldsCollection.split(", ");

Once you have array, you iterate and get the array elements

–Regards,

Nagesh

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 12, 2015, 5:51am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/7 "2015-11-12T05:51:40Z")

</div>

Hi [@nagesh](https://community.apigee.com/users/56/nageswaralingalacognizantcom.html) thanks for that answer, though it is the same kind of workaround as getting the query fields form context.getVariable(“request.querystring”); Either way still have to parse the feilds, at least with [@Dino](https://community.apigee.com/users/803/dchiesa.html)’s answer you’d write less code and handle the parsing of the query params to the URL.js library.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 12, 2015, 6:18am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/8 "2015-11-12T06:18:28Z")

</div>

I’m assuming that apigee has some Java layer underneath it and that’s probably the reason why this function would return a JavaObject in javascript. It’s still accessible in JS but not as a standard JS collection. I assume we would have to know what the methods are of the netscape (I believe?) wrapper on Java objects. [@Dino](https://community.apigee.com/users/803/dchiesa.html) so far has the best solution, but it still does not answer my 3 questions listed above.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 12, 2015, 4:41pm UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/9 "2015-11-12T16:41:15Z")

</div>

Yes, that is correct. The Javascript you write runs within Rhino, which is hosted in a Java VM. The thing you are getting is a Java object, an instance of java.util.Set.

> it it still does not answer my 3 questions listed above.

True enough! I’ll address your questions but can’t promise you satisfaction.

> why is that?

I can’t answer that, I don’t know. It just is. It was designed this way.

> is this what is meant by a Collection

I suppose so.

> how do I iterate the query params?

try this:

```auto
var queryParamNames = context.getVariable("request.queryparams.names");
var a = queryParamNames.toArray();
var length = a.length;
context.setVariable('test_qparam_names_length', length);
var i = 0;
a.forEach(function(item) {
  context.setVariable('test_qparam_name_found' + i++, true);
  context.setVariable('test_qparam_name_' + i++, item.toString());
});

```

For my own purposes, it is easier to use the string manipulation approach, or to use the URI.js library. YMMV.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 15, 2015, 7:34am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/10 "2015-11-15T07:34:35Z")

</div>

Thanks [@Dino](https://community.apigee.com/users/803/dchiesa.html) for the answers, they are to my satisfaction. So basically if the JavaObject returned is a JS wrapper of the Java Collection interface then there is a way we could leverage this. As you posted in your example of using .toArray(); which is a Java method in the Colleciton interface, we might as well can use .contains(); to check if a parameter is found inside it. This what I needed to do anyways.

So actually if you check out [http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html](http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html) and use those exposed methods, you can use the Set more efficiently. This should be reflected in the documentation too.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 16, 2015, 9:43pm UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/11 "2015-11-16T21:43:12Z")

</div>

Yes, [@Georges Haddad](https://community.apigee.com/users/2968/georgeshaddad.html) , good suggestion. We should update the documentation appropriately. I agree completely that the current statement - “this variable returns a variable of type _Collection_” - needs to be clearer. We will do that and get back to you.

---

<div class="post-metadata">

**Author:** ![anilsr](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@anilsr](https://discuss.google.dev/u/anilsr)\
**Post date:** [November 17, 2015, 4:34am UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/12 "2015-11-17T04:34:03Z")

</div>

[@docs](https://community.apigee.com/users/809/docs4g.html) [@sgilson](https://community.apigee.com/users/213/sgilsonapigeecom.html) [@Floyd Jones](https://community.apigee.com/users/202/fjonesapigeecom.html), For your reference, Can we provide a link to this discussion page from [Variable Reference Documentation](http://apigee.com/docs/api-services/reference/variables-reference) page in “message.queryparams.names” section & update docs as needed? Thank you 🙂

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 17, 2015, 2:07pm UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/13 "2015-11-17T14:07:12Z")

</div>

Thank you [@Anil Sagar](https://community.apigee.com/users/368/asagarapigeecom.html), [@Dino](https://community.apigee.com/users/803/dchiesa.html), and [@Georges Haddad](https://community.apigee.com/users/2968/georgeshaddad.html)! I’m working on this documentation update and will post to this inquiry once the content has been published. Appreciate all of the help with identifying the fix.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [September 4, 2017, 11:28pm UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/14 "2017-09-04T23:28:38Z")

</div>

Hi,

I’ve found that the ‘array’ given by

```auto
var tmp = context.getVariable("request.queryparams.names");
var qp = qparams.toArray();

```

is indeed an iterable array; so doing

```auto
// this *does* print the first element
print("qp[0]: " + qp[0]);
// but..
print("type of param: " + typeof(q2[0]);
// prints 'object', not 'string'
print("type of param: " + typeof(q2[0].toString());
// also.

if ( q2[0] == 'xx' ) {..}
// Always fails (actually I'm using Sets to do compulsory/optional param validation but this holds)

*However*... if you do

if (q2[0] + "" == 'xx')

then you win.

I'm happy and disturbed...

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [April 26, 2018, 2:36pm UTC](https://discuss.google.dev/t/how-do-you-iterate-collection-from-request-queryparams-names-in-js/12968/15 "2018-04-26T14:36:56Z")

</div>

The URI.js library has a function, hasQuery(queryparamname) function that returns true if the queryparamname exists in the URI query string.
