[racket] What is a comparable rkt code for curl command with HTTP Auth?

From: J G Cho (gcho at fundingmatters.com)
Date: Sun Jan 8 15:38:39 EST 2012

Thanks for the tip on base64-encode newline 'feature'. Kinda stuff
that takes long to figure out on one's own.

As for inspecting what curl is sending out, I modified the code from
More: Systems Programming with Racket tutorial a bit:

(define (handle in out)
  (copy-port in out)
  (display "bye" out))

Voila! I get back:

Authorization: Basic YWJjOmVmZw==
User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7
OpenSSL/0.9.8r zlib/1.2.3
Host: localhost:8080
Accept: */*
Content-Type: application/json
Content-Length: 64

{"score": 1337, "playerName": "Sean Plott", "cheatMode": false }

(I digress but the last line does not seem to get executed; "bye" is missing. )

Anyhow, my code now looks like:

(define (my-header u p)
  (let ([auth (bytes->string/utf-8
               (regexp-replace #rx#"[\r\n]+$"
                               (base64-encode
                                (string->bytes/utf-8
                                 (format "~a:~a" u p)))
                               ""))])
    (list "POST /echo HTTP/1.1"
          (format "Authorization: Basic ~a" auth)
          "User-Agent: curl/7.19.7 (universal-apple-darwin10.0)
libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3"
          "Accept: */*")))


(define (save-remote! data)
  (let ([URL (string->url "https://pixbyfunc.appspot.com/echo")]
        [DATA (string->bytes/utf-8
               (alist->form-urlencoded `((msg . ,(format "~a" data))
(a . "a"))))]
        [HEADER (my-header "user" "pwd")])
    ;port is closed automatically
    (call/input-url URL
                    (lambda (url)
                      (post-pure-port url DATA HEADER))
                    (lambda (port)
                      (port->string port)))))

(save-remote! "hello-world")


I do get back "hello-world". How exciting, eh? (I had to hide json
part for the current 'echo' server and content related header.)  A
step closer to interacting with Parse.com, it seems.

As far as Wireshark goes (I believe this is the second time you
recommended it), I will give a try at some point. It just happened
that I re-read the tutorial this morning and the material seemed to
make more sense so I gave it a try.

Thanks a ton.

jGc


On Sun, Jan 8, 2012 at 1:35 AM, Neil Van Dyke <neil at neilvandyke.org> wrote:
> Two things to do:
>
> 1. "base64-encode" produces a newline at the end, which you probably want to
> remove somehow:
>
> #lang racket/base
> (require net/base64)
>
> (base64-encode #"user:pwd")
> ;==> #"dXNlcjpwd2Q=\r\n"
>
> (regexp-replace #rx#"[\r\n]+$" (base64-encode #"user:pwd") "")
> ;==> #"dXNlcjpwd2Q="
>
> 2. Your debugging will be much easier if you can see the actual protocol
> between client and server.  My favorite tool for this is Wireshark, which is
> Free Software.  Then you can compare the protocol of your "curl" example
> with your Racket one.
>
> --
> http://www.neilvandyke.org/



Posted on the users mailing list.