I'm trying to make a POST request to a Plug router using HTTPoison. My GET requests are successful, it is only the POST that is failing. I'm not sure if the problem is Plug or 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
And the client:
# client.ex
host = "http://localhost:8030"
host
|> Path.join("/myserver/url")
|> HTTPoison.post!(Poison.encode!(%{}))
|> IO.inspect()
The response I get is a 500. I'm not sure why, since the route in the router is configured to accept POSTs. Does anyone know what I'm doing wrong?
UPDATE
People have asked for the stack trace. There isn't one. But this is the 500 response:
..%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
}