我正在编写 Elixir (1.8) + Plug_Cowboy (2.0.2) + Jason (1.1.2) 服务器。从我从文档中获得的信息来看,一旦 Plug 解析器通过,我应该将所有内容都放在body_params
. 问题是conn.body_params
在我的情况下访问会返回%Plug.Conn.Unfetched{aspect: :body_params}
. 检查下面的代码:
defmodule Test.Router do
use Plug.Router
require Logger
plug :match
plug Plug.Parsers, parsers: [:json],
pass: ["application/json", "text/json"],
json_decoder: Jason
plug :dispatch
post "/test" do
Logger.debug inspect(conn.body_params)
conn
|> put_resp_content_type("text/plain")
|> send_resp(204, "Got it")
end
end
Run Code Online (Sandbox Code Playgroud)
知道发生了什么吗?
我用以下方法测试:
curl -H "Content-Type: text/json" -d "{one: 1, two: 2}" 127.0.0.1:8080/test
Run Code Online (Sandbox Code Playgroud)
我曾尝试添加:urlencoded
解析器,或重新排列插件顺序,但无济于事。
Plug.Parsers.JSON
只处理application/json
内容类型。这就是body_params
没有被填充的原因。您的测试 JSON 也无效 - 未引用对象属性名称。
curl -H "Content-Type: application/json" -d '{"one": 1, "two": 2}' 127.0.0.1:8080/test
Run Code Online (Sandbox Code Playgroud)
该pass:
选项Plug.Parsers
指示它忽视这些类型其中没有定义的解析器(如请求text/json
在您的情况),而不是抛出UnsupportedMediaTypeError
,这就是它通常会做的。添加该选项隐藏了错误。
您可能会发现它use
Plug.Debugger
在开发过程中很有用- 它将为您提供有关意外情况下发生的事情的更好信息。
如果出于某种原因您需要能够使用非标准text/json
类型解析 JSON (例如,由于来自您无法控制的软件的请求),您可以为该类型定义一个新的解析器,该解析器只是包装Plug.Parsers.JSON
并重写text/json
为application/json
:
defmodule WrongJson do
def init(opts), do: Plug.Parsers.JSON.init(opts)
def parse(conn, "text", "json", params, opts) do
Plug.Parsers.JSON.parse(conn, "application", "json", params, opts)
end
def parse(conn, _type, _subtype, _params, _opts), do: {:next, conn}
end
Run Code Online (Sandbox Code Playgroud)
然后将其添加到您的解析器列表中:
plug Plug.Parsers,
parsers: [:json, WrongJson],
json_decoder: Jason
Run Code Online (Sandbox Code Playgroud)
现在text/json
也将工作。
归档时间: |
|
查看次数: |
1142 次 |
最近记录: |