# Reading request\_body before passing to backend server

**URL:** <https://www.ruby-forum.com/t/reading-request-body-before-passing-to-backend-server/228897>\
**Category:** NGINX\
**Created:** [May 12, 2013, 3:54am UTC](https://www.ruby-forum.com/t/reading-request-body-before-passing-to-backend-server/228897 "2013-05-12T03:54:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dktp1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@dktp1](https://www.ruby-forum.com/u/dktp1)\
**Post date:** [May 12, 2013, 3:54am UTC](https://www.ruby-forum.com/t/reading-request-body-before-passing-to-backend-server/228897/1 "2013-05-12T03:54:31Z")

</div>

I am trying to read if a specific value is set (passed) as a POST using  
request\_body.

What I am trying to do is:

server {  
…  
if ($request\_body ~ “API-value-Iwanttocheck”) {  
set $my\_api “TRUE”;  
}

And later on pass to the backend server:

location / {  
proxy\_pass [http://backend:80](http://backend:80);  
}

However, that never seems to match. I even enabled logging and modified  
my  
log\_format to have:

"  
“POSTREQUEST:$request\_body”  
"

And I can see in the log when I don’t have the if statement. When I add  
the  
if statement, it doesn’t show up in the logs any more.

Anyone have any idea? Or a simple way to check if a value set via POST  
is  
actually there?

Posted at Nginx Forum:

> **[Reading request\_body before passing to backend server](https://forum.nginx.org/read.php?2,239091,239091#msg-239091)**
>
> I am trying to read if a specific value is set (passed) as a POST using request\_body. What I am trying to do is: server { .. if ($request\_body ~ "API-value-Iwanttocheck") { set $my\_api "TRUE"; } And

---

<div class="post-metadata">

**Author:** ![dktp1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@dktp1](https://www.ruby-forum.com/u/dktp1)\
**Post date:** [May 13, 2013, 9:09am UTC](https://www.ruby-forum.com/t/reading-request-body-before-passing-to-backend-server/228897/2 "2013-05-13T09:09:34Z")

</div>

Hello!

On Sat, May 11, 2013 at 09:53:47PM -0400, dktp1 wrote:

> "  
> “POSTREQUEST:$request\_body”  
> "
> 
> And I can see in the log when I don’t have the if statement. When I add the  
> if statement, it doesn’t show up in the logs any more.
> 
> Anyone have any idea? Or a simple way to check if a value set via POST is  
> actually there?

The $request\_body variable isn’t available in rewrites as request  
body isn’t yet read at this phase.

Request body can be read and examined using embedded perl module,  
see here:

> **[Module ngx\_http\_perl\_module](http://nginx.org/en/docs/http/ngx_http_perl_module.html)**

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**
