[erlang-questions] url query string encode/decode

Loïc Hoguin essen@REDACTED
Fri Dec 15 14:22:34 CET 2017

On 12/15/2017 12:39 PM, Roger Lipscombe wrote:
> On 15 December 2017 at 10:55, Caragea Silviu <silviu.cpp@REDACTED> wrote:
>> Seems parsing of lists is not compliant.
> There's no standard to be "compliant" with. Everyone pretty much
> agrees on key-value pairs where the keys are unique. Beyond that, who
> knows? I've seen ?a=1&a=2&a=3 and ?a[]=1&a[]=2&a[]=3, for instance.
> See, e.g., https://stackoverflow.com/q/24059773/8446.

The keys are not unique actually. Browsers will actually send query 
strings with duplicate keys if there are duplicate names in forms. 
That's actually how PHP's name[]=value thing works, you get many name[] 
keys in the query string.

Cowboy will happily return duplicate keys whether you use 
cowboy_req:parse_qs (which calls the equivalent function in cowlib) or 
cowboy_req:match_qs, in the latter case you get a map like #{name => 
[<<"a">>, <<"b">>, <<"c">>]}. It doesn't require the [] or any other 
indicator for this. (But if you want to name your key 'name[]', no 
problem either.)

> fwiw, I tend to build my URLs with hackney_url. I don't know what it
> does with repeated keys; I've never tried.

Loïc Hoguin

More information about the erlang-questions mailing list