[racket] Http post using post-impure-port

From: Daniel Phelps (phelps.da at gmail.com)
Date: Fri Jun 11 18:08:14 EDT 2010

Hi All,
Thanks for your help.  Solution follows.  Thanks for the pointer to netcat.

Turns out I was missing the Content-Type.  Hence:

#lang racket
(require net/url)
(require net/uri-codec)
(require net/head)
(require web-server/http/request-structs)
(require racket/string)

(define my-url
  (string->url "http://localhost:49986/Foo/Bar"))

(define (bytes-> str)
  (string->bytes/locale str))

(define (form-data str)
  (bytes-> str))

(define (post url data)
   (form-data data)
   '("Content-Type: application/x-www-form-urlencoded"
     "User-Agent: HackitWithRacket")))

(post my-url "foo=bar&fiz=buz")

Which produced the following:

C:\Users\Daniel\bxscripts>nc -l -p 49986
POST /Foo/Bar HTTP/1.0
Host: localhost:49986
Content-Length: 36
Content-Type: application/x-www-form-urlencoded
User-Agent: HackitWithRacket


And it works.  Fun.  I hope this helps somebody.


On Fri, Jun 11, 2010 at 2:42 PM, Eli Barzilay <eli at barzilay.org> wrote:
> On Jun 11, Noel Welsh wrote:
>> I did a quick check of the source code and post-impure-port definitely
>> does send the post data -- at least functions to do so appear in the
>> code. Have you tried a packet sniffer like Wireshark? That will tell
>> exactly was is being sent over the wire.
> Netcat is very simple -- I ran
>  nc -l -p 49986
> and the script sent exactly this:
>  POST /foo/bar/ HTTP/1.0
>  Host: localhost:49986
>  Content-Length: 15
>  foo=bar&fiz=fuz
> --
>          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
>                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.