Reputation: 383
I am writing a small http server using Elixir Plug and having some trouble understanding how its parser works. I cannot figure out how to access the result of the parser in my router. I currently have
...
plug Plug.Parsers, parsers: [:json],
json_decoder: Poison
plug :match
plug :dispatch
forward "/admin" , to: VoucherSite.Admin.AdminRouter
...
And in the admin router I want to access the parsed result from the Parser called above
put "/user" do
...access result here...
respond(conn, {:ok, ""})
end
It feels like I am missing something obvious but all I have access to is conn
which is the connection so no idea how to get the parsed body from the request.
Upvotes: 2
Views: 860
Reputation: 121000
Plug
is a behaviour
, having 2 callbacks: init/1
and call/2
. The latter receives a Plug.Conn
struct and returns possibly modified Plug.Conn
struct. That allows to chain plugs.
%Plug.Conn{assigns}
is intended to be used to update the map between calls to different plugs.
Plug.Conn
implements the Inspect
algebra out of the box, so just do:
Logger.debug inspect(conn)
somewhere and you’ll see where the parser stores the body (I would bet it’s some key in the assigns
map.)
Upvotes: 0