[plt-scheme] Re: net collection: bindings -> post-bytes?

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue May 11 13:40:41 EDT 2010

#lang racket
(require net/uri-codec)

(define (string->post-bytes s)
  (string->bytes/utf-8
   (form-urlencoded-encode ; Here's the important encoding function
    s)))

(define symbol->post-bytes
  (compose string->post-bytes symbol->string))

(define bindings->post-bytes
  (match-lambda
    [(list) #""]
    [(list (list (? symbol? sym) (? string? val)))
     (bytes-append (symbol->post-bytes sym) #"=" (string->post-bytes val))]
    [(list-rest (list (? symbol? sym) (? string? val)) bs)
     (bytes-append (symbol->post-bytes sym) #"=" (string->post-bytes val)
                   #"&" (bindings->post-bytes bs))]))

(bindings->post-bytes `((name "bob") (age "14")))

On Tue, May 11, 2010 at 11:34 AM, John Clements
<clements at brinckerhoff.org> wrote:
> Perhaps we're missing something obvious here: if we want to send a set of bindings, e.g.
>
> `((name "bob") (age 14))
>
> as a POST request, is there a standard encoding function that maps an alist to a 'bytes?'  such as
>
> #"name=bob&age=14"
>
> (e.g. for use with post-pure-port) in such a way that the receiving end can use the standard request->bindings functions? It wouldn't be hard to roll our own, but I'm sure there are a standard set of quoting conventions that it would be nice to conform to.
>
> ObDueDiligence: We checked out the HTTP section of the net collection docs, without useful result.
>
> Any help (including RTFMs) appreciated,
>
> John
>
>



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

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


Posted on the users mailing list.