Trouble shooting net/http-client

26 views
Skip to first unread message

je back

unread,
May 15, 2020, 3:57:52 PM5/15/20
to Racket Users
I'm having trouble with formatting the #:data parameter to a http-sendrecv request.  If I insert the string literal after #:data "my string" it works, if I bind the string to a symbol (define my-string "my string") and call with #:data my-string i get an error from the severs: HTTP/1.1 400 Bad Request .  Is there a way to echo or examine the request as sent?

Also, I can't figure out where the source code for this library is located.

Thanks,
Jan Erik


Sam Tobin-Hochstadt

unread,
May 15, 2020, 4:08:36 PM5/15/20
to je back, Racket Users
The source code is here:
https://github.com/racket/racket/blob/master/racket/collects/net/http-client.rkt

Sam
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/b786e6fa-512b-4694-885a-afde1a011cf1%40googlegroups.com.

Jon Zeppieri

unread,
May 15, 2020, 4:15:27 PM5/15/20
to je back, Racket Users
I can't reproduce this with a simple example.

```
#lang racket/base

(require net/http-client)

(define data "hello world!")

(http-sendrecv "postman-echo.com"
"/post"
#:ssl? #f
#:method #"POST"
#:data data)
```

Maybe you could post your code?

- Jon


On Fri, May 15, 2020 at 3:57 PM je back <jeb...@gmail.com> wrote:
>

je back

unread,
May 15, 2020, 8:47:35 PM5/15/20
to Racket Users
Sam: Thanks for the link to the source

Jon: I didn't include an example in my original post because the call is for obtaining an authorization token from a private server which made it awkward to share. But, thanks to your example, I pursued coming up with a good example and managed to get my original snippet working. I've pasted the  example with the test server I used below.  I found this server (httpbin.org) helpful because it echos the data element that it received -- this enabled me to sort out my typos...

Jan Erik

#lang racket
(require net/http-client json)
(let* ((user "test_user")
        (password "test_password")
        (ht (hasheq 'client_id "test_client" 'grant_type "Bearer" 'username user 'password password)))
  (define-values (status headers in)
      (http-sendrecv "httpbin.org"
                     "/post"
                     #:ssl? #t
                     #:version "1.1"
                     #:method "POST"
                     #:data (jsexpr->string ht)
                     #:headers (list "Content-Type: application/json")))
    (displayln status)
    (displayln headers)
    (displayln (port->string in))
    (close-input-port in))
Reply all
Reply to author
Forward
0 new messages