Getting this strange undecipherable error on a get request using the API passthrough

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.

2 Likes