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

From: Daniel MacDougall (dmacdougall at gmail.com)
Date: Mon Sep 12 14:29:41 EDT 2011

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

$ 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.



And here's the server code:

#lang racket

(require web-server/servlet-env

(define-values (app-dispatch app-url)
     [("") 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))
(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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110912/b246145b/attachment.html>

Posted on the users mailing list.