# Trouble getting the Request Body of a HTTP Post

**URL:** <https://www.ruby-forum.com/t/trouble-getting-the-request-body-of-a-http-post/180463>\
**Category:** NGINX\
**Created:** [December 19, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/trouble-getting-the-request-body-of-a-http-post/180463 "2009-12-19T15:25:42Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Tronman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tronman](https://www.ruby-forum.com/u/Tronman)\
**Post date:** [December 19, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/trouble-getting-the-request-body-of-a-http-post/180463/4 "2009-12-19T15:25:42Z")

</div>

Hello!

On Thu, Dec 17, 2009 at 01:58:00PM -0500, Tronman wrote:

> to be run when the Request Body is read (yes, I did mean Request  
> Body, sorry for my typo). Since the response is dependant on the  
> body, “ngx\_http\_my\_module\_handler” really doesn’t have to do  
> anything else after setting the call back but return NGX\_DONE.

Yes.

But please note that once ngx\_http\_read\_client\_request\_body()  
returns something \>= NGX\_HTTP\_SPECIAL\_RESPONSE you should return  
it from handler instead of NGX\_DONE. Basically it means that your  
body handler was already called (since full body was already  
read from client) and you have to pass it’s return code. So code  
in handler should look like:

```
rc = ngx_http_read_client_request_body(r,
                            ngx_http_my_module_body_handler);

if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
    return rc;
}

return NGX_DONE;

```

See e.g. proxy module for an example.

> Thus, when the function “ngx\_http\_my\_module\_body\_handler” gets  
> called, I’ve got both the original headers, and the request  
> body, and it’s here that I should place all of the logic to  
> build my response and send it to the client.

Yes.

> This implies that the client program connecting to the server  
> must use separate “send” calls to send the header first, then  
> the body, and cannot put both a header and a body into the same  
> buffer and send them at the same time. Is this the case?

No. As long as http request is correct it doesn’t matter if it  
was send in one packet or not.

> > To see the difference between (2) and (3) you may consider  
> > using some big POST (e.g. 10M) slowly uploaded by client. This  
> > also should give you some idea about how to handle multiple  
> > request body buffers and disk-buffered data.
> 
> In this case, what would be the order of function calls? For  
> example, would “ngx\_http\_my\_module\_body\_handler” be called for  
> each piece of the body (which I would write my own logic to  
> handle) or called after the entire (say 10M) body has been read.

It’s called once entire body has been read.

> Is “ngx\_http\_my\_module\_handler” still called only once when the header arises?

Yes.

> In the meantime, I have moved my response generating logic into  
> the “ngx\_http\_my\_module\_body\_handler” which is working much  
> better (for single chunk bodies at least). Also, when I was  
> telnet-ing, I was trying to send headers and body in one chunk,  
> which wasn’t working. Instead I started sending them in separate  
> chunks and am getting closer to the behaviour I expected.

Most likely you did something wrong, either while telneting or in  
you module.

Maxim D.

---

_[View the full topic](https://www.ruby-forum.com/t/trouble-getting-the-request-body-of-a-http-post/180463)._
