在 Elixir/Phoenix 中解析 multipart/mixed 请求的 json 部分
Parsing a json part of a multipart/mixed request in Elixir/Phoenix
我正在编写一个 Elixir/Phoenix json API 后端,并希望端点能够接收多个图像文件以及单个 json 格式的属性要求。我为此使用 multipart/mixed
内容类型,这是我的 cURL 请求的样子。
curl -4 -H "Content-Type: multipart/mixed" \
-H "Authorization: FNRM8ISXiUujh+I2X" \
-F "meta={"registration": "face"}; type=application/json" \
-F "captures[]=@r1.jpg; type=image/jpg" \
-F "captures[]=@r2.jpg; type=image/jpg" \
-X POST http://localhost:4000/api/bioid/register -i -v
在我的控制器操作中,我收到了经过整齐解析的文件,但我的 json 部分未被解析。
pry(2)> params
%{
"captures" => [
%Plug.Upload{
content_type: "image/jpg",
filename: "r1.jpg",
path: "/var/folders/v7/qlf90zb13szch162hvx6d1s00000gn/T//plug-1530/multipart-1530206341-21611204340048-7"
},
%Plug.Upload{
content_type: "image/jpg",
filename: "r2.jpg",
path: "/var/folders/v7/qlf90zb13szch162hvx6d1s00000gn/T//plug-1530/multipart-1530206341-700143184604965-7"
}
],
"meta" => "{registration: face}; type=application/json"
}
这是一个似乎相关的讨论,WIP: How to parse multipart form with JSON field #570. It makes it apparent that the parsing of the application/json
parts should not be expected to happen automatically. Instead the last message suggests using a custom body reader 执行任何额外的解析。
这个解释是否正确,或者是否有另一种方法来处理 multipart/mixed
有效负载,以便来自 application/json
部分的任何参数都可以在已经解析的控制器操作中使用?
附带问题
让我感到困惑的是,我什至不必使用 Plug.Parsers
就能进行上述解析。根据我的阅读,以下内容对于获取经过解析的多部分有效负载应该是必需的。
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
json_decoder: Poison
无论管道中是否存在此插件,我都会得到相同的结果。它可能默认存在,但我想确认一下。
我正在使用 Elixir 1.6.5、Phoenix 1.3.2 和 Plug 1.6.0。
Plug 多部分解析器似乎按预期工作。 Plug JSON 解析器显然只适用于内容类型为 application/json
或 application/*+json
的请求,参见 source code。我假设解析器正在查看整个请求的内容类型,并且不知道多部分请求部分。
据我所知,目前最简单的解决方案是自己解析内容。我可以看到您希望它如何工作,并且可能值得通过 Github 问题请求此功能。
我正在编写一个 Elixir/Phoenix json API 后端,并希望端点能够接收多个图像文件以及单个 json 格式的属性要求。我为此使用 multipart/mixed
内容类型,这是我的 cURL 请求的样子。
curl -4 -H "Content-Type: multipart/mixed" \
-H "Authorization: FNRM8ISXiUujh+I2X" \
-F "meta={"registration": "face"}; type=application/json" \
-F "captures[]=@r1.jpg; type=image/jpg" \
-F "captures[]=@r2.jpg; type=image/jpg" \
-X POST http://localhost:4000/api/bioid/register -i -v
在我的控制器操作中,我收到了经过整齐解析的文件,但我的 json 部分未被解析。
pry(2)> params
%{
"captures" => [
%Plug.Upload{
content_type: "image/jpg",
filename: "r1.jpg",
path: "/var/folders/v7/qlf90zb13szch162hvx6d1s00000gn/T//plug-1530/multipart-1530206341-21611204340048-7"
},
%Plug.Upload{
content_type: "image/jpg",
filename: "r2.jpg",
path: "/var/folders/v7/qlf90zb13szch162hvx6d1s00000gn/T//plug-1530/multipart-1530206341-700143184604965-7"
}
],
"meta" => "{registration: face}; type=application/json"
}
这是一个似乎相关的讨论,WIP: How to parse multipart form with JSON field #570. It makes it apparent that the parsing of the application/json
parts should not be expected to happen automatically. Instead the last message suggests using a custom body reader 执行任何额外的解析。
这个解释是否正确,或者是否有另一种方法来处理 multipart/mixed
有效负载,以便来自 application/json
部分的任何参数都可以在已经解析的控制器操作中使用?
附带问题
让我感到困惑的是,我什至不必使用 Plug.Parsers
就能进行上述解析。根据我的阅读,以下内容对于获取经过解析的多部分有效负载应该是必需的。
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
json_decoder: Poison
无论管道中是否存在此插件,我都会得到相同的结果。它可能默认存在,但我想确认一下。
我正在使用 Elixir 1.6.5、Phoenix 1.3.2 和 Plug 1.6.0。
Plug 多部分解析器似乎按预期工作。 Plug JSON 解析器显然只适用于内容类型为 application/json
或 application/*+json
的请求,参见 source code。我假设解析器正在查看整个请求的内容类型,并且不知道多部分请求部分。
据我所知,目前最简单的解决方案是自己解析内容。我可以看到您希望它如何工作,并且可能值得通过 Github 问题请求此功能。