How to retrive body as a map in Plug Cowboy

I have added this to my router

  plug(Plug.Parsers,
    parsers: [:json],
    pass: ["*/*"],
    json_decoder: Jason

  

Now in my post handle view

  IO.inspect(conn.params)
    # get post result body
    {:ok, body, conn} = Plug.Conn.read_body(conn)
    IO.inspect( Jason.decode!(body))

I try to retrive the body sent to the post request but it’s giving me an error.

 localhost:8000 (http)
Request: POST /auth/session/post
** (exit) an exception was raised:
    ** (Jason.DecodeError) unexpected byte at position 0: 0x73 ("s")

Plug.Parsers already handles decoding the incoming JSON into conn.params (and conn.body_params); what’s the intent of the read_body here?

What was the body sent with this request? Seems odd that it would start with s :thinking:

2 Likes