LeakyBucket

LeakyBucket

Accessing the Request Body in Plug

I have a question about getting consistent access to the Request Body in Plug.

According to the documentation for Plug.Conn.read_body the body is consumed once it is accessed.

If you need to access the body multiple times, it is your responsibility to store it. Finally keep in mind some plugs like Plug.Parsers may read the body, so the body may be unavailable after being accessed by such plugs.

I’m trying to figure out a good way to build a Plug that plays nicely in an ecosystem where downstream code relies on Plug.Parsers handling JSON but some routes need to verify a signature on the raw request body? Is this possible at all or do I need to put a service in front of everything to block unsigned requests to the protected endpoints?

Most Liked

easco

easco

Do you really need to rely on Plug.Parser to handle the JSON for you… or could your verification plug read the body and put the JSON into the Conn in the same place that Plug.Parsers does (so it takes up the responsibility of validating what it needs to AND parsing the JSON)?

sribe

sribe

It’s not that hard to add your own plug in the pipeline first which reads the body and stashes a copy (or reads it and validates a signature). Sorry I don’t have access anymore to my code that does that.

Last Post!

suprafly

suprafly

This was a really useful post for me, thank you!

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement