Elixir URI.encode_query 嵌套映射

标签 elixir

灵药URI.encode_query在非嵌套 map 上效果很好,例如:

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

但是如果我尝试编码嵌套 map ,比如
URI.encode_query(%{ a: %{ b: "c" } })

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

我如何使用嵌套 map 对查询进行编码?

最佳答案

灵药URI模块不支持嵌套映射,但您可以使用 plug包的Plug.Conn.Query.encode/1确实支持嵌套 map :

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"

关于Elixir URI.encode_query 嵌套映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43487513/

相关文章:

elixir - Plug 路由器是否需要匹配/调度管道?

pattern-matching - 如何在 elixir 函数中使用匹配的参数

heroku - 如何将 Phoenix 控制台 (iex) 与 heroku 一起使用?

shell - AWS ECS故障-运行Shell脚本来启动程序

postgresql - 将当前时间的默认值设置为 Ecto 迁移中的日期时间列

phoenix-framework - 将子模型的 id 插入父模型 Elixir/Phoenix

Elixir-同一行上有多个表达式-使用do : syntax in function definition时发生编译器错误

elixir - 如何检查文件是图像还是ascii 文本文件?

elixir - 克隆 Ecto 记录。带有嵌入和相关记录?

decimal - 在 Elixir 中舍入十进制数