[racket] question about client side http post

From: Jordan Schatz (jordan at noionlabs.com)
Date: Mon Jan 2 22:38:20 EST 2012

It looks correct to me, except for port-string should be port->string
(note the ->).

The string that the server returns will depend on what the server
responds, so your server code would need to echo the value or give some
other confirmation, if that is how you are verifying that the data was

Also you need a relatively recent version of racket for https to work, I
think its only the current and previous point releases that support https
with post-pure-port.

I tested your code like so and it worked for me:

Server code, in separate file and running
#lang web-server

(require web-server/servlet-env)

(define (test-for-and-display-binding req)
  (let* ([bindings (request-bindings req)])
    (if (exists-binding? 'key bindings)
        (extract-binding/single 'key bindings)

(define (start req)
   `(html (head (title "Echo!"))
          (body (p ,(test-for-and-display-binding req))))))

(serve/servlet start
               #:servlet-path "/echo"
               #:launch-browser? #t
               #:port 8080)

Your code, with the port->string corrected and the url to match the 
#lang racket

(require net/url)

(define (save-remote! data)
  (let ([url (string->url "http://localhost:8080/echo")]
        [data (string->bytes/utf-8  (format "key=~a" data))])
    (port->string (post-pure-port url data))))

(save-remote! "hello-world")

Works for me... 

On Mon, Jan 02, 2012 at 05:03:20PM -0500, J G Cho wrote:
> I am trying to do client side HTTP POST using
> (require net/url)
> (post-pure-port URL post [header]) → input-port?
>   URL : url?
>   post : bytes?
>   header : (listof string?) = null
> The following throws no exception but does not seem to do the intended
> job (I used Chrome to test the remote URL's get and post. So I am sure
> it works on the server side).
> (require net/url)
> (define (save-remote! data)
>   (define URL  (string->url "https://example.com/some-method?"))
>   (define DATA (string->bytes/utf-8  (format "key=~a" data)))
>   (port-string (post-pure-port URL DATA)))
> What am I doing wrong here?
> jGc
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.