I've followed this issue to get the raw body from a POST in my controller but the body only shows up if the encoding is set to application/x-www-form-urlencoded
. When I test with curl
I can read the POST body but the body shows up empty when the encoding is set to text/xml
. In my router.ex
I have:
pipeline :api do
plug :accepts, ["xml"]
end
In my controller I have:
def parse(conn, params) do
xml = conn.private[:raw_body]
Logger.debug(xml)
text conn, xml
end
In endpoint.ex
:
def copy_req_body(conn, _) do
Plug.Conn.put_private(conn, :copy_raw_body, true)
end
plug :copy_req_body
I'm new to Phoenix and Elixir so I'm not sure how to debug this. Why does the encoding of the HTTP request make a difference to the reading of the raw body data? My application needs to accept text/xml
and read the request body as a string.
Pretty simple way. Not sure if it's "wrong" or not though.
defmodule YourApp.YourController do
use YourApp.Web, :controller
def receive_obm(conn, params) do
{:ok, body, _conn} = Plug.Conn.read_body(conn)
IO.inspect(body)
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With