Why does Plug.Conn.Query.encode replace space with + instead of %20?

It seems that Plug.Conn.Query.encode() converts the space character to “+”. From what I can tell, that is an old (sub) standard for URL param encoding.

%20 is the correct replacement according to https://tools.ietf.org/html/rfc3986

Does anyone know why this is the way it is in Plug?


You might use :http_uri.encode/1 to escape " " with "%20".

Plug.Conn.Query.encode/1 behaves this way because of URI.encode_www_form/1, which intentionally replaces %20 with +, which is valid in application/x-www-form-urlencoded.

iex> URI.encode_www_form(" ")
iex> :http_uri.encode(" ")


1 Like

There is already mentioned URI module with lots of functions to work with. One of them is https://hexdocs.pm/elixir/URI.html#encode/2 which does percent-escape.