tags:

views:

162

answers:

3

I am having trouble on finding authoritative information about the behavior with HTTP GET query string duplicate fields, like

http://example.com/page?field=foo&field=bar

and in particular if the order is kept or not. Most web-oriented languages produce an array containing both foo and bar associated to a key "field", but I would like to know if authoritative statement exist (e.g. on a RFC) about this point. RFC 3986 has a section 3.4. Query, which refers to key=value pairs, but nothing is said on how to interpret order and duplicate fields and so on. This makes sense, since it's backend dependent, and not in the scope of that RFC...

Although a de-facto standard exists, I'd like to see an authoritative source for it, just out of curiosity.

A: 

Typically, duplicate parameter values like

http://example.com/page?field=foo&field=bar

result in a single queryString parameter that is an array:

field[0]=='foo'
field[1]=='bar'

I've seen this behavior in ASP, ASP.NET and PHP4.

David Lively
exactly, this is the de-facto standard, but as far as I see there's no authoritative decision on it. Since I don't believe this is the case, I am just inept to find it.
Stefano Borini
Yes, probably everyone has seen that behaviour. The question was if that is actually specified somewhere.
Thilo
+2  A: 

Most (none?) of the frameworks offer no guarantees, so assume they will be returned in random order.

Always take the safest approach.

For example, java HttpServlet interface: ServletRequest.html#getParameterValues

Even the getParameterMap method leaves out any mention about parameter order (the order of a java.util.Map iterator cannot be relied on either.)

Photodeus
+1  A: 

There is no spec on this. You may do what you like.

Typical approaches include: first-given, last-given, array-of-all, string-join-with-comma-of-all.

Suppose the raw request is:

GET /blog/posts?tag=ruby&tag=rails HTTP/1.1
Host: example.com

Then there are various options for what request.query['tag'] should yield, depending on the language or the framework:

request.query['tag'] => 'ruby'
request.query['tag'] => 'rails'
request.query['tag'] => ['ruby', 'rails']
request.query['tag'] => 'ruby,rails'
Justice
More to the point of the question, there is also the option of ['rails', 'ruby'] (different order).
Thilo
One can certainly do a great number of things.
Justice