I am writing a POST request that should send some information to a REST api using elixir, this information should be able to be accessed in the conn.body_params, however it appears empty.
My code is as follows:
conn2 = conn(:post, gameID<>"/guesses", Poison.encode!(%{guess: "p"}))
|> GameRouter.call()
assert conn2.status == 201
My plug also has the following configuration:
plug Plug.Parsers, parsers: [:urlencoded, :multipart, :json], pass: ["*/*"], json_decoder: Poison
How should it be written as to actually send the information in the POST request?
You need to add the content-type: application/json
header so that Plug.Parsers
will know to use the json parser.
conn2 =
conn(:post, gameID <> "/guesses", Poison.encode!(%{guess: "p"}))
|> put_req_header("content-type", "application/json")
|> GameRouter.call()
The pass: ["*/*"]
is telling Plug.Parsers
to ignore all content types it doesn't know about. If you remove that, you will get Plug.Parsers.UnsupportedMediaTypeError
, which would have helped you identify the problem.