Elixir URI.encode_query嵌套映射

时间:2017-04-19 05:55:45

标签: elixir

Elixir URI.encode_query适用于非嵌套地图,例如:

URI.encode_query(%{ a: "b", c: "d" }) # => "a=b&c=d"

但如果我尝试编码嵌套地图,比如

URI.encode_query(%{ a: %{ b: "c" } })

我得到了

** (Protocol.UndefinedError) protocol String.Chars not implemented for %{b: "c"}

我怎么能用嵌套地图编码查询呢?

1 个答案:

答案 0 :(得分:8)

Elixir的URI模块不支持嵌套地图,但您可以使用支持嵌套地图的plugPlug.Conn.Query.encode/1

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"
相关问题