Hi,

I've been using `URI.encode_query/1` to convert a key/value map to a query 
string to be appended to a URL (as GET parameters).

As it turns out, `URI.encode_query/1` encodes differently than 
`URI.encode/2`, particularly in the case of spaces ("+" instead of "%20"). 
For more context, please see the comment thread here:

https://github.com/elixir-lang/elixir/pull/2392

For me, it was non-obvious that I better *not* use `encode_query` to encode 
a URL-appending query string. I tried using `URI.encode/2` instead, as this 
provides RFC3986 encoding, but this simply behaves differently, in the 
sense that it takes string input, not an Enumerable.

Given the above, I'd like to propose the addition of `URI.encode_query/2`, 
where the second argument should allow the user to specify which type of 
encoding should be used.
Default encoding should be the existing `:www_form` encoding (I'm not sure 
if there is an official RFC for www-form-urlencoded? If so, we should use 
that)
One should be able to replace this with RFC3986, e.g. like so:

URI.encode_query(%{foo: "bar"}, :rfc3986)

This should produce a query string with spaces encoded as "%20" instead of 
"+" (and possibly other differences).

An alternative approach could be to use an `opts` Keyword list:

URI.encode_query(%{foo: "bar"}, encoding: :rfc3986)

The latter approach could have the benefit of being able to add other 
options later (which I currently cannot think of, but others might).

The general benefit for Elixir users would be to be able to encode Elixir 
data structures (typically maps) into RFC3986 compliant query strings 
without having to manually iterate over every item and/or patching the 
`URI.encode_query/1` result with quick-and-dirty solutions like 
`String.replace(query_string, "+", "%20")`.

That's it. I'd love to hear feedback!

– Floris

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/538f2618-dd14-4084-b6cc-dabb7a8f58e7n%40googlegroups.com.

Reply via email to