URI.encode_query
encode_query
, go back to URI module for more information.
Specs
Encodes enumerable
into a query string using encoding
.
Takes an enumerable that enumerates as a list of two-element
tuples (for instance, a map or a keyword list) and returns a string
in the form of key1=value1&key2=value2...
.
Keys and values can be any term that implements the String.Chars
protocol with the exception of lists, which are explicitly forbidden.
You can specify one of the following encoding
strategies:
:www_form
- (default, since v1.12.0) keys and values are URL encoded as perencode_www_form/1
. This is the format typically used by browsers on query strings and form data. It encodes " " as "+".:rfc3986
- (since v1.12.0) the same as:www_form
except it encodes " " as "%20" according RFC 3986. This is the best option if you are encoding in a non-browser situation, since encoding spaces as "+" can be ambiguous to URI parsers. This can inadvertently lead to spaces being interpreted as literal plus signs.
Encoding defaults to :www_form
for backward compatibility.
Examples
iex> query = %{"foo" => 1, "bar" => 2}
iex> URI.encode_query(query)
"bar=2&foo=1"
iex> query = %{"key" => "value with spaces"}
iex> URI.encode_query(query)
"key=value+with+spaces"
iex> query = %{"key" => "value with spaces"}
iex> URI.encode_query(query, :rfc3986)
"key=value%20with%20spaces"
iex> URI.encode_query(%{key: [:a, :list]})
** (ArgumentError) encode_query/2 values cannot be lists, got: [:a, :list]