[racket] Building a server that responds to POST/PUT/DELETE requests

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue Sep 13 10:58:49 EDT 2011

I just pushed a fix to these issues.

Problem 1 was that I only read request bodies on POSTs.

Problem 2 was that I misread the spec and didn't see that request
bodies had to have Content-Length fields.

Jay

On Mon, Sep 12, 2011 at 12:29 PM, Daniel MacDougall
<dmacdougall at gmail.com> wrote:
> Thanks for the reply. See below for the server code I'm using.
> I narrowed down the problem a bit and there are actually two different but
> related issues. The first is that request data is ignored for non-POST,
> non-PUT requests:
> $ curl http://localhost:8000 -d "abcd" -X POST
> <html><p>POST</p><p>abcd</p></html>
> $ curl http://localhost:8000 -d "abcd" -X PATCH
> <html><p>PATCH</p><p>No request body</p></html>
> The second problem is that POST and PUT requests hang when no request body
> is specified:
> $ curl http://localhost:8000 -X POST (hangs)
> $ curl http://localhost:8000 -X PUT (hangs)
> No server errors, even after killing the request. I imagine the server is
> waiting on the request stream to parse a request body that will never come.
> Daniel
> ---
> And here's the server code:
> #lang racket
> (require web-server/servlet-env
>          web-server/http/xexpr
>          web-server/http/request-structs
>          web-server/dispatch)
> (define-values (app-dispatch app-url)
>     (dispatch-rules
>      [("") index]
>      [else index]))
> (define (index req)
>   (response/xexpr `(html (p ,(bytes->string/utf-8 (request-method req)))
>                          (p ,(cond ((bytes? (request-post-data/raw req))
>                                     (bytes->string/utf-8
> (request-post-data/raw req)))
>                                    (else "No request body"))))))
> (serve/servlet app-dispatch
>                #:port 8000
>                #:servlet-regexp #rx""
>                #:command-line? #t)
> On Mon, Sep 12, 2011 at 5:50 AM, Jay McCarthy <jay.mccarthy at gmail.com>
> wrote:
>>
>> This sounds strange to me. Request objects have a 'method' field that
>> you can look at it. The bindings are a promise so that non-form-data
>> POST content doesn't get erroneously parsed as bindings (unless you
>> force the promise). This is all in place so that you can implement
>> which ever methods you want without interference from the server's
>> default behavior. Can you post a small example that has the hanging
>> error?
>>
>> Jay
>>
>> On Sat, Sep 10, 2011 at 8:12 PM, Daniel MacDougall
>> <dmacdougall at gmail.com> wrote:
>> > Hi,
>> > I'm trying to build a web server that responds to [POST/PUT/DELETE]
>> > requests. If I use the built-in web server and POST to a url I have
>> > defined
>> > a dispatch function for, the request hangs indefinitely. The docs say
>> > that
>> > the web server tries to hide the details of the request method from you.
>> > Is
>> > there any way to work around this? Or am I limited to defining GET
>> > requests
>> > with continuations?
>> > Thanks,
>> > Daniel
>> > _________________________________________________
>> >  For list-related administrative tasks:
>> >  http://lists.racket-lang.org/listinfo/users
>> >
>>
>>
>>
>> --
>> Jay McCarthy <jay at cs.byu.edu>
>> Assistant Professor / Brigham Young University
>> http://faculty.cs.byu.edu/~jay
>>
>> "The glory of God is Intelligence" - D&C 93
>
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93



Posted on the users mailing list.