Elixir URI.encode_query
works well on not-nested maps, like:
URI.encode_query(%{ a: "b", c: "d" }) # => "a=b&c=d"
But if I try to encode nested map, like
URI.encode_query(%{ a: %{ b: "c" } })
I get
** (Protocol.UndefinedError) protocol String.Chars not implemented for %{b: "c"}
How could I encode query with nested map in it?
Elixir's URI
module does not support nested maps but you can use the plug
package's Plug.Conn.Query.encode/1
which does support nested maps:
iex(1)> Plug.Conn.Query.encode %{ a: "b", c: "d" }
"a=b&c=d"
iex(2)> Plug.Conn.Query.encode %{ a: %{ b: "c" } }
"a[b]=c"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With