wfgilman

wfgilman

How do you put a request body in a Plug.Conn?

I’m writing a Plug which verifies a signature in the header of a request. The signature is the SHA256 HMAC hash of the request body signed with a secret. I’m having trouble testing this because I don’t know how to put the request body in the %Conn{} to run it through the plug. Setting up the headers are easy with put_req_header/3. Is there an equivalent for setting the body?

I’m trying to setup my connection like this in my test:

setup %{conn: conn} do
  body = %{"some" => "message"}
  signature = Plug.Crypto.MessageVerifier.sign(Poison.encode!(body), "s3cret", :sha256)
  conn =
    conn
    |> put_req_header("accept", "application/json")
    |> put_req_header("content-type", "application/json")
    |> put_req_header("x-request-signature", signature)
    |> put_request_body???
  {:ok, conn: conn}
end

Marked As Solved

wfgilman

wfgilman

This ended up being the solution to my problem: How to: Plug which verifies header signature signed using request body

Also Liked

idi527

idi527

Would something like this conn = %{conn | body_params: body} work for you?

setup %{conn: conn} do
  body_params = %{"some" => "message"}
  signature =
    body_params
    |> Poison.encode!()
    |> Plug.Crypto.MessageVerifier.sign("s3cret", :sha256)
  conn =
    conn
    |> put_req_header("accept", "application/json")
    |> put_req_header("content-type", "application/json")
    |> put_req_header("x-request-signature", signature)
    |> put_body_params(body_params)
  {:ok, conn: conn}
end

defp put_body_params(conn, body_params) do
  %{conn | body_params: body_params}
end

Or as a workaround you might put your body in conn.private.

flash4syth

flash4syth

I tried to use this solution to populate a %Plug.Conn{}'s body_params key; however, the conn.body_params was set to %{} after calling conn = post(conn, "/path") in my test. However, when I simply wrote it thus:

post(conn, “/path”, %{“my_params” => “param_val”, …})

Then it worked like a charm–conn.body_params was populated.

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement