{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
3
Question by Georges Haddad · Nov 10, 2015 at 05:45 AM · 5.7k Views javascriptrequest param

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

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.

Comment
Add comment Show 8
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anil Sagar @ Google ♦♦   · Nov 10, 2015 at 06:14 AM 0
Link

Dear @Georges Haddad , Welcome to Apigee Community. We see the same results. Let us look into it.

avatar image Anil Sagar @ Google ♦♦ Anil Sagar @ Google ♦♦   · Nov 10, 2015 at 04:31 PM 0
Link

@krupa , Any idea what is the root cause ?

avatar image Georges Haddad · Nov 12, 2015 at 06:18 AM 0
Link

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 so far has the best solution, but it still does not answer my 3 questions listed above.

avatar image Dino ♦♦ Georges Haddad   · Nov 12, 2015 at 04:41 PM 1
Link

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:

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.

avatar image Georges Haddad Dino ♦♦ · Nov 15, 2015 at 07:34 AM 0
Link

Thanks @Dino 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 and use those exposed methods, you can use the Set more efficiently. This should be reflected in the documentation too.

Show more comments

Close

4 Answers

  • Sort: 
avatar image
5
Best Answer

Answer by Dino   · Nov 10, 2015 at 08:12 PM

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"
// }
Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Georges Haddad · Nov 11, 2015 at 04:48 AM 0
Link

Thanks @Dino, 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.

avatar image
0

Answer by nagesh · Nov 12, 2015 at 05:08 AM

Hi @Georges Haddad

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

Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Georges Haddad · Nov 12, 2015 at 05:51 AM 0
Link

Hi @nagesh 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's answer you'd write less code and handle the parsing of the query params to the URL.js library.

avatar image
0

Answer by fredericvanderelst · Sep 04, 2017 at 11:28 PM

Hi,

I've found that the 'array' given by

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

is indeed an iterable array; so doing

// 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...
Comment
Add comment · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Kipruto Bett · Apr 26, 2018 at 02:36 PM

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

Comment
Add comment · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Not able to get body content in javascript policy 1 Answer

Access Javascript variable in Service Callout ploicy 1 Answer

How to call two services using httpclient? 1 Answer

Asynchronous HTTP Requests in an API proxy to another API proxy 2 Answers

How to access httpClient response headers in javascript? 2 Answers

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges