How can override not parsed conn body

I am trying to override the original body in conn, but it seems like I cannot figure out how to do that.

def create_stuff(conn) do
  {:ok, body, _conn} = Plug.Conn.read_body(conn, length: 1_000_000)
  json_body = Jason.decode!(body)
  data = get_stuff_from_another_service(json_body)
  new_data = merge_body_with_another_data(json_body, data)

  # How can I update the original body of the connection?
  conn = %{conn | ...: Jason.encode!(new_data)}
  # After that I proxy the connection to another services
  ReverseProxy.call(conn, [])
end

Is it possible to replace the initial body?

1 Like

You might want to look into the :body_reader option, but this is not going to be the cleanest code ever, as the body is meant to be read on demand and read once straight from the adapter.

Can you refactor ReverseProxy so that you can pass the body explicitly?

1 Like

Thank you for the link, but I decided to simply handle the request with HTTPoison and do not deal directly with cowboy_req.read_req_body. I simply cannot find how the raw body is set even in the adapter.