[racket] web server: CRUD put/delete?

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Thu Dec 4 19:58:41 EST 2014

It would have been interesting to see what you tried. The simplest
thing I could think of worked fine. Here's a little database app:

#lang racket/base
(require web-server/http
         web-server/servlet-env
         web-server/dispatch)

(define (writer s)
  (λ (op)
    (displayln s op)))

(define db (make-hash))
(define (put! req what)
  (hash-set! db what (request-post-data/raw req))
  (response/output (writer "Resource updated.")))
(define (get req what)
  (if (hash-has-key? db what)
      (response/output (writer (hash-ref db what)))
      (response/output #:code 404
                       (writer "Resource not in database."))))
(define (delete! req what)
  (hash-remove! db what)
  (response/output (writer "Resource deleted.")))

(define-values (go go-url)
  (dispatch-rules
   [("resources" (string-arg)) #:method "put"
    put!]
   [("resources" (string-arg)) #:method "get"
    get]
   [("resources" (string-arg)) #:method "delete"
    delete!]))

(module+ main
  (serve/servlet
   go
   #:port 6892
   #:command-line? #t
   #:servlet-regexp #rx""))

% curl -X GET http://localhost:6892/resources/spam
Resource not in database.
% curl -X PUT -d "Spam Spam" http://localhost:6892/resources/spam
Resource updated.
% curl -X GET http://localhost:6892/resources/spam
Spam Spam
% curl -X DELETE http://localhost:6892/resources/spam
Resource deleted.
% curl -X GET http://localhost:6892/resources/spam
Resource not in database.
%

On Thu, Dec 4, 2014 at 6:49 PM, George Neuner <gneuner2 at comcast.net> wrote:
> Hi all,
>
> I'm using 6.0.1 trying to implement a CRUD interface, but  the
> dispatch-rules doesn't seem to recognize PUT or DELETE as valid methods -
> they both are interpreted as GET.
>
> Is dispatch-rules limited to HTML 1.0 (i.e. only GET, POST and HEAD)?
>
> Has this been updated since 6.0.1?  The current 6.1 docs for dispatch-rules
> appear to be the same.
>
> Thanks,
> George
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



-- 
Jay McCarthy
http://jeapostrophe.github.io

           "Wherefore, be not weary in well-doing,
      for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
                          - D&C 64:33


Posted on the users mailing list.