json/2
executes send_resp/3
under the hood (see code), so your code is attempting to send 2 responses.
Why itâs erroring out is explained in Plug.Conn.send_resp/1
Note that this function does not halt the connection, so if subsequent plugs try to send another response, it will error out. Use
halt/1
after this function if you want to halt the plug pipeline.
Since json/2
is executing send_resp/3
but doesnât implicitly halt the connect, your code is trying to send another response on a connection thatâs already :sent
and therefore crashes with a frankly quite cryptic error.
At least I think thatâs whatâs happening, but looking at the code for send_resp
it should be raising an AlreadySent
error? Iâm not sure if my analysis of the error is correct. But in any case, try to remove the code after json(conn, %{data: "yabadadoo"})
and see what happens.