Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir URI.encode_query nested map

Tags:

elixir

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?

like image 531
asiniy Avatar asked Apr 19 '17 05:04

asiniy


1 Answers

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"
like image 190
Dogbert Avatar answered Nov 12 '22 13:11

Dogbert