1 HTTP Client
(require net/http-client) | package: base |
procedure
(http-conn? x) → boolean?
x : any/c
procedure
(http-conn-live? x) → boolean?
x : any/c
procedure
(http-conn) → http-conn?
procedure
(http-conn-open! hc host [ #:ssl? ssl? #:port port]) → void? hc : http-conn? host : (or/c bytes? string?) ssl? : (or/c boolean? ssl-client-context? symbol?) = #f port : (between/c 1 65535) = (if ssl? 443 80)
If hc is live, the connection is closed.
procedure
(http-conn-open host [ #:ssl? ssl? #:port port]) → http-conn? host : (or/c bytes? string?) ssl? : (or/c boolean? ssl-client-context? symbol?) = #f port : (between/c 1 65535) = (if ssl? 443 80)
procedure
(http-conn-close! hc) → void?
hc : http-conn?
procedure
(http-conn-abandon! hc) → void?
hc : http-conn?
procedure
(http-conn-send! hc uri [ #:version version #:method method #:close? close? #:headers headers #:content-decode decodes #:data data]) → void? hc : http-conn-live? uri : (or/c bytes? string?) version : (or/c bytes? string?) = #"1.1" method : (or/c bytes? string? symbol?) = #"GET" close? : boolean? = #f headers : (listof (or/c bytes? string?)) = empty decodes : (listof symbol?) = '(gzip) data : (or/c false/c bytes? string? data-procedure/c) = #f
If data is a procedure, it will be called once with a procedure of one argument, which is a string or byte string to be written to the request body using chunked transfer encoding.
If headers does not contain an Accept-Encoding header, then a header indicating that encodings from decodes are accepted is automatically added.
If close? is #t and headers does not contain a Connection header, then a Connection: close header will be added.
This function does not support requests that expect 100 (Continue) responses.
procedure
(http-conn-recv! hc [ #:content-decode decodes #:close? close?])
→
bytes? (listof bytes?) input-port? hc : http-conn-live? decodes : (listof symbol?) = '(gzip) close? : boolean? = #f
Returns the status line, a list of headers, and an port which contains the contents of the response.
If close? is #t, then the connection will be closed following the response parsing. If close? is #f, then the connection is only closed if the server instructs the client to do so.
procedure
(http-conn-sendrecv! hc uri [ #:version version #:method method #:headers headers #:data data #:content-decode decodes #:close? close?])
→
bytes? (listof bytes?) input-port? hc : http-conn-live? uri : (or/c bytes? string?) version : (or/c bytes? string?) = #"1.1" method : (or/c bytes? string? symbol?) = #"GET" headers : (listof (or/c bytes? string?)) = empty data : (or/c false/c bytes? string? data-procedure/c) = #f decodes : (listof symbol?) = '(gzip) close? : boolean? = #f
procedure
(http-sendrecv host uri [ #:ssl? ssl? #:port port #:version version #:method method #:headers headers #:data data #:content-decode decodes])
→
bytes? (listof bytes?) input-port? host : (or/c bytes? string?) uri : (or/c bytes? string?) ssl? : (or/c boolean? ssl-client-context? symbol?) = #f port : (between/c 1 65535) = (if ssl? 443 80) version : (or/c bytes? string?) = #"1.1" method : (or/c bytes? string? symbol?) = #"GET" headers : (listof (or/c bytes? string?)) = empty data : (or/c false/c bytes? string? data-procedure/c) = #f decodes : (listof symbol?) = '(gzip)
The HTTP connection is not returned, so it is always closed after one response, which is why there is no #:closed? argument like http-conn-recv!.
1.1 Troubleshooting and Tips
1.1.1 How do I send properly formatted POST form requests?
You should send a Content-Type header with the value application/x-www-form-urlencoded and send the data formatted by net/uri-codec’s form-urlencoded-encode function. For example,
(http-send! hc "/login" #:method "POST" #:data (alist->form-urlencoded (list (cons 'username "Ryu") (cons 'password "Sheng Long"))) #:headers (list "Content-Type: application/x-www-form-urlencoded"))