Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a spec for nested urlencoded params[]?

At one point in history, server side languages started tweaking urlencoded parameters to add support for submitting data as arrays and key/value objects:

// key/value pairs
contact[name]=John
contact[phone]=800-555-1234

// arrays
foo[]=bar
foo[]=baz

I’m playing around with some edge cases in one library's nested parameter parsing, eg preservation of parameter order. Is there a spec that formalizes how a server should process this encoding? If not, which reference implementation introduced this syntax?

like image 261
Brad Koch Avatar asked May 15 '15 14:05

Brad Koch


1 Answers

Square brackets in URIs

According to RFC-3986 "Uniform Resource Identifier (URI): Generic Syntax" using unencoded square brackets in URIs isn't allowed. Therefore it's not a HTTP standard.

A host identified by an Internet Protocol literal address, version 6 [RFC3513] or later, is distinguished by enclosing the IP literal within square brackets ("[" and "]"). This is the only place where square bracket characters are allowed in the URI syntax.

Many programming languages are using square brackets for arrays, therefore I guess it was a natural carry-over to use square brackets in URIs.

Is there a spec that formalizes how a server should process this encoding?

No, at least not language independent.

First Implementation

A lot of web application frameworks allow reuse of the same key in the query to create an array, for example http://example.org/?foobar=hello&foobar=world. These frameworks also allow to use square brackets in keys, but it makes no difference, they are just part of the name. Associative arrays are rarely supported. (Caveat: I'm not familiar with all this languages.)

  • Perl, 1987: Numbered Arrays are supported, but not associative arrays.
  • PHP, 1995: Supports numbered arrays as well as associative arrays.
  • Active Server Pages (ASP), 1996: Numbered Arrays are supported, but not associative arrays.
  • JavaServerPages (JSP), 1999: Numbered Arrays are supported, but not associative arrays.
  • JAX-RS (Java), 2008: Numbered Arrays are supported, but not associative arrays.

So far PHP is the only language I've found, which supports associative arrays in URLs without custom code.

like image 102
Christian Strempfer Avatar answered Nov 19 '22 20:11

Christian Strempfer