0

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
}
dopatraman
  • 13,416
  • 29
  • 90
  • 154
  • Post the full error stack trace of your Router. – Sheharyar Nov 02 '18 at 20:18
  • That's not possible, there has to be a stack trace or the code you've provided is incomplete. [See this](https://github.com/sheharyarn/ex_gun/blob/master/lib/ex_gun/web/router.ex) on how to write a plug router. – Sheharyar Nov 02 '18 at 21:23
  • 1
    Try using [`Plug.Debugger`](https://hexdocs.pm/plug/Plug.Debugger.html) to get a useful error message. – Adam Millerchip Nov 03 '18 at 13:17

1 Answers1

-1

The problem is with this line:

send_resp(200, Poison.encode!(stuff))

You haven't defined a variable named stuff, so it throws an exception which returns a 500 Internal Server Error.

Sheharyar
  • 73,588
  • 21
  • 168
  • 215