我正在尝试使用 HTTPoison 向 Plug 路由器发出 POST 请求。我的 GET 请求成功,只有 POST 失败。我不确定问题是 Plug 还是 HTTPoison:
# server.ex
defmodule MyServer do
use Application
use Plug.Router
import Plug.Conn, only: [put_resp_header: 3, send_resp: 3]
alias Plug.Adapters.Cowboy
plug(:match)
plug(Plug.Parsers,
parsers: [:json],
pass: ["*/*", "application/json", "text/plain"],
json_decoder: Poison
)
plug(:dispatch)
post "/myserver/url" do
conn
|> send_resp(200, Poison.encode!(%{}))
end
def start(_type, _args) do
port = Application.get_env(:server, :cowboy_port, 8030)
IO.puts("Started server on http://localhost:#{port}/")
children = [
Cowboy.child_spec(:http, __MODULE__, [], port: port)
]
Supervisor.start_link(children, strategy: :one_for_one)
end
end
和客户:
# client.ex
host = "http://localhost:8030"
host
|> Path.join("/myserver/url")
|> HTTPoison.post!(Poison.encode!(%{}))
|> IO.inspect()
我得到的响应是 500。我不知道为什么,因为路由器中的路由配置为接受 POST。有谁知道我做错了什么?
更新
人们要求堆栈跟踪。没有一个。但这是 500 响应:
..%HTTPoison.Response{
body: "",
headers: [
{"server", "Cowboy"},
{"date", "Fri, 02 Nov 2018 20:57:01 GMT"},
{"content-length", "0"}
],
request_url: "http://localhost:8030/myserver/url"
status_code: 500
}