[racket] Web server and file upload/download examples

From: Danny Yoo (dyoo at hashcollision.org)
Date: Mon Oct 8 13:31:23 EDT 2012

On Mon, Oct 8, 2012 at 4:56 AM, Erich Rast <erich at snafu.de> wrote:
> Hi,
>
> I'm not very experienced with using the web server yet and am looking
> for advice and examples of how to download and upload files. I'm
> particularly interested in any example of uploading a file using a
> racket client (not a browser) to the web server, because it seems hard
> to understand this from just the docs alone.

On the server side of things, I believe we can use bindings-assq on an
HTTP request's collection of name-value binding pairs.  The value we
get back from bindings-assq can be a binding:file structure.

Mirroring the example from
http://docs.racket-lang.org/web-server/http.html#(def._((lib._web-server/http/request-structs..rkt)._request-bindings/raw)),
an example that expects file input would look something like this:

;;;;;;;;;;;;;;;;;;;;;;
(require web-server/http)

;; print-file-content: request -> void
(define (print-file-content req)
  (match (bindings-assq #"file" (request-bindings/raw req))
    [(struct binding:file (id filename headers content))
     (printf "I got a file with content: ~s\n" content)]))
;;;;;;;;;;;;;;;;;;;;;;


On the client side of things, it looks like we want to send a
"multipart/form-data" encoding of the file's bytes over as the body of
a post-pure-port.

The details on how to build this thing should already be handled by a
library...  However, I'm a little disconcerted: I can't seem to find a
Racket library on PLaneT or the standard library that handles this
task.  Basically, I'm looking for the counterpart of the net/uri-codec
functions, but for encoding to the mulitpart/form-data format.  I must
be missing something?

Posted on the users mailing list.