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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement