Doumi.URI.Query - A helper library that supports encoding complicated query of URI

It’s common to send nested maps or lists via query string even though it is not standardized.

URI.encode_query/2 does not support nested maps or lists and can’t be customized to handle that.

Doumi.URI.Query supports encoding maps or lists to query string. (only PHP-style now)

iex> query = %{"foo" => %{"bar" => 1, "baz" => 2}}
iex> Doumi.URI.Query.encode(query)
"foo%5Bbar%5D=1&foo%5Bbaz%5D=2" # foo[bar]=1&foo[baz]=2

iex> query = %{"foo" => [1, 2, 3]}
iex> Doumi.URI.Query.encode(query)
"foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3" # foo[]=1&foo[]=2&foo[]=3

Thanks!

great timing, I was just about to have to write this for my project!

1 Like

This is just a convention, but not officially spec‘ed behaviour. Therefore this is not part of URI. But Plug comes with a helper for de/encoding those nested values: Plug.Conn.Query — Plug v1.14.0

1 Like

Thanks!
Taking encoder via params looks good.