Best way to accept multiple values in a query parameter

I want to know the best way we can have in API to accept multiple values for a query parameter.

For example if I want to pass multiple values for a query parameter name then I can do it like -

1. <API URL>?name=a,b,c- Use some logic to from an array and take this ahead for processing.

2. <API URL>?name=a&name=b&name=c - Use some logic to from an array and take this ahead for processing.

Above are just examples and there might be more possible ways to do that.

I want to know what is the best way we can use to make use of API simpler.

Thanks,

Santosh

Solved Solved
1 5 31.5K
1 ACCEPTED SOLUTION

akoo
New Member

Hi Santosh,

Great question. As I'm sure you are aware, RFC 3986 (Uniform Resource Identifier (URI): Generic Syntax) doesn't cover this aspect of query parameters in section 3.4. You've identified the 2 most common ways to pass in multiple values for a query parameter.

From an Apigee Edge perspective, the main difference is that request.queryparam.<queryParamName>.values.count will be 1 for #1 and 3 for #2. To access the values in case #1, you can use request.queryparam.name directly, and in case #2 you will need to use request.queryparam.name.values. In both cases, you'll likely need to do some massaging to manipulate the data.

Aside from that, you may want to consider what is easier for your client to send to Apigee.

View solution in original post

5 REPLIES 5

@santosh_ghalsasi , Great question. See similar discussion in S.O here.

akoo
New Member

Hi Santosh,

Great question. As I'm sure you are aware, RFC 3986 (Uniform Resource Identifier (URI): Generic Syntax) doesn't cover this aspect of query parameters in section 3.4. You've identified the 2 most common ways to pass in multiple values for a query parameter.

From an Apigee Edge perspective, the main difference is that request.queryparam.<queryParamName>.values.count will be 1 for #1 and 3 for #2. To access the values in case #1, you can use request.queryparam.name directly, and in case #2 you will need to use request.queryparam.name.values. In both cases, you'll likely need to do some massaging to manipulate the data.

Aside from that, you may want to consider what is easier for your client to send to Apigee.

Hi @Alex Koo,

Here if I consider #2 way of sending information from client. Could I know how we can access each value in this query param?

Thanks in advance!

Hi Karthick,

The variable request.queryparam.<QPName>.values is an array.

You can slice and dice the array as you'd like in a script callout, e.g., javascript callout:

 arr = context.getVariable('request.queryparam.qp1.values');
 context.setVariable('newName',arr[1]);

Feel free to upvote my original answer if you found this useful 🙂

This suggestion no longer works, because of new restrictions on the code that runs within a JavaScript callout. Instead you need to coerce the resulting value to a string, and then parse the string.

Example

var arr = context.getVariable('request.queryparam.qp1.values');
arr = arr + ''; //coerce the array to a string 
var parts = arr.slice(1,-1).map(function(x) { return x.trim(); });
context.setVariable('newName',parts[1]);