Correct way to pass multiple values for same parameter name in GET request

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?


Solution 1:

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.

Solution 2:

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.