我正在尝试构建一个 phoenix 应用程序,并处理一个 POST 请求。我想获取请求正文,但似乎找不到任何有关如何执行此操作的文档。
做一些逆向工程,我得到了以下代码:
defmodule MyApp.Controllers.Pages do
use Phoenix.Controller
def post(conn) do
{_, {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, body, _, _, _, _, _, _}} = conn.adapter
text conn, "#{inspect body}"
end
end
带路由:
defmodule MyApp.Router do
use Phoenix.Router
post "/test", MyApp.Controllers.Pages, :post
end
一定有更好的方法,不是吗?
预期行为:
curl -XPOST localhost:4000/test -d 'this is a test'
$ "this is a test"