Reputation: 5019
I'm looking into what is the correct way to pass multiple values for the same parameter name in a GET request.
I've seen URLs like this:
http://server/action?id=a&id=b
And I've seen URLs like this:
http://server/action?id=a,b
My understanding is that the first is correct, but I can't find any reference for this. I had a look at the http spec but couldn't see anything about how the 'query' part of a URL should be made up.
I don't want an answer that says "either is fine" - if I'm building a webservice, I want to know which of these methods is standard so that people using my webservice know how to pass multiple parameters for the same name.
So, can someone point me at an official reference source to confirm which option is correct?
Upvotes: 361
Views: 474048
Reputation: 35
Since the url is single parameter and multiple values. A very simple solution in Java is to split the string and then append it to self. For example below:
String baseUrl = "http://server/action"
String id = "a,b";
StringBuilder url = new StringBuilder();
url = baseUrl.append("?");
String[] idArr = id.split(",");
StringBuilder sb = new StringBuilder();
for ( String fetchId : idArr) {
sb.append("&id=").append(fetchId);
url.append(sb);
}
Upvotes: -2
Reputation: 445
My answer is more PHP-oriented.
Submitting multi-value form fields, i.e. submitting arrays, can be done in several different ways, as a standard is not necessarily spelled out.
Three possible ways to send multi-value fields or arrays would be:
For Example :
http://localhost:3000/foo?id[]=a&id[]=b
Returns
Array
(
[id] => Array
(
[0] => a
[1] => b
)
)
(NOTE: In this case, it would be important to name query key to some_name[], so that the resulting request vars would be registered as an array by PHP)
Upvotes: 6
Reputation: 5436
Solutions above didn't work. It simply displayed the last key/value pairs, but this did:
http://localhost/?key[]=1&key[]=2
Returns:
Array
(
[key] => Array
(
[0] => 1
[1] => 2
)
Upvotes: 3
Reputation: 63
there is no standard, but most frameworks support both, you can see for example for java spring that it accepts both here
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam List<String> id) {
return "IDs are " + id;
}
And Spring MVC will map a comma-delimited id parameter:
http://localhost:8080/api/foos?id=1,2,3
----
IDs are [1,2,3]
Or a list of separate id parameters:
http://localhost:8080/api/foos?id=1&id=2
----
IDs are [1,2]
Upvotes: 2
Reputation: 1657
I am describing a simple method which worked very smoothly in Python (Django Framework).
1. While sending the request, send the request like this
http://server/action?id=a,b
2. Now in my backend, I split the value received with a split function which always creates a list.
id_filter = id.split(',')
Example: So if I send two values in the request,
http://server/action?id=a,b
then the filter on the data is
id_filter = ['a', 'b']
If I send only one value in the request,
http://server/action?id=a
then the filter outcome is
id_filter = ['a']
3. To actually filter the data, I simply use the 'in' function
queryset = queryset.filter(model_id__in=id_filter)
which roughly speaking performs the SQL equivalent of
WHERE model_id IN ('a', 'b')
with the first request and,
WHERE model_id IN ('a')
with the second request.
This would work with more than 2 parameter values in the request as well !
Upvotes: 16
Reputation: 821
I would suggest looking at how browsers handle forms by default. For example take a look at the form element <select multiple>
and how it handles multiple values from this example at w3schools.
<form action="/action_page.php">
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
For PHP use:
<select name="cars[]" multiple>
Live example from above at w3schools.com
From above if you click "saab, opel" and click submit, it will generate a result of cars=saab&cars=opel. Then depending on the back-end server, the parameter cars should come across as an array that you can further process.
Hope this helps anyone looking for a more 'standard' way of handling this issue.
Upvotes: 36
Reputation: 3449
Indeed, there is no defined standard. To support that information, have a look at wikipedia, in the Query String chapter. There is the following comment:
While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field.[3][4]
Furthermore, when you take a look at the RFC 3986, in section 3.4 Query, there is no definition for parameters with multiple values.
Most applications use the first option you have shown: http://server/action?id=a&id=b
. To support that information, take a look at this Stackoverflow link, and this MSDN link regarding ASP.NET applications, which use the same standard for parameters with multiple values.
However, since you are developing the APIs, I suggest you to do what is the easiest for you, since the caller of the API will not have much trouble creating the query string.
Upvotes: 284