I am trying to debug some stuff in one of my controllers and I need access to the raw post body before it is consumed by parsers (namely the json parser). I tried doing this: https://hexdocs.pm/plug/Plug.Parsers.html#module-custom-body-reader and adding some IO.puts inside the CacheBodyReader, but that didn’t print anything to the console like I would expect (meaning it isn’t running). Here is the relevant code (in my router.ex
):
defmodule CacheBodyReader do
def read_body(conn, opts) do
{:ok, body, conn} = Plug.Conn.read_body(conn, opts)
IO.puts("TESTING")
conn = update_in(conn.assigns[:raw_body], &[body | (&1 || [])])
{:ok, body, conn}
end
end
pipeline :paypal_webhook do
plug Plug.Parsers,
parsers: [:urlencoded, :json],
pass: ["text/*"],
body_reader: {CacheBodyReader, :read_body, []},
json_decoder: Jason
plug PhilomenaWeb.PaypalWebhookValidatorPlug
end
Is there something I’m missing? I have IO.puts in the PaypalWebhookValidatorPlug call
function and those are printing just fine (but by then the connection body is empty as it has been consumed by the json parser and it’s been merged into conn.params). The fact that I see those puts tells me my pipeline is being executed. If anyone could give me some info that would be great. Thanks.