http-client
(require http-client) | package: http-client |
1 Example
> (http-get "https://httpbin.org" #:path "anything/fruits" #:data (hasheq 'color "red" 'made-in "China" 'price 10) #:headers (hasheq 'Accept "application/json" 'Token "temp-token-abcef")) tcp-connect: connection failed;
host not found
address: httpbin.org
port number: 443
system error: Temporary failure in name resolution;
gai_err=-3
> (define httpbin-org (http-connection "https://httpbin.org/anything" (hasheq 'Content-Type "application/json" 'Accept "application/json") (hasheq 'made-in "China" 'price 10)))
> (http-bin-org 'get #:path "/fruits" #:data (hasheq 'color "red") #:headers (hasheq 'Token "temp-token-abcef")) http-bin-org: undefined;
cannot reference an identifier before its definition
in module: top-level
> (http-post httpbin-org #:data (hasheq 'color "red") #:path "/fruits" #:headers (hasheq 'Token "temp-token-abcef")) tcp-connect: connection failed;
host not found
address: httpbin.org
port number: 443
system error: Temporary failure in name resolution;
gai_err=-3
> (http-post httpbin-org ; modify the headers to do the post using html form format. #:headers (hasheq 'Content-Type "application/x-www-form-urlencoded")) tcp-connect: connection failed;
host not found
address: httpbin.org
port number: 443
system error: Temporary failure in name resolution;
gai_err=-3
> (define new-conn (struct-copy http-connection httpbin-org ; copying and modifying a predefined conn and headers to do a post using html form. [headers (hasheq 'Content-Type "application/x-www-form-urlencoded")])) (http-post new-conn) tcp-connect: connection failed;
host not found
address: httpbin.org
port number: 443
system error: Temporary failure in name resolution;
gai_err=-3
> (define res (http-post "https://httpbin.org/anything" #:data (hasheq 'color "red"))) (http-response-body res) ; the body of a response is auto converted to the racket type data unless you set current-http-response-auto. res: undefined;
cannot reference an identifier before its definition
in module: top-level
> (parameterize ([current-http-response-auto #f]) ; set current-http-response-auto to #f to get a raw format http response body. (define res (http-post "https://httpbin.org/anything" #:data (hasheq 'color "red"))) (http-response-body res)) tcp-connect: connection failed;
host not found
address: httpbin.org
port number: 443
system error: Temporary failure in name resolution;
gai_err=-3
2 Reference
parameter
(current-http-user-agent v) → void? v : string?
= string
parameter
(current-http-response-auto v) → void? v : boolean?
= #t
struct
(struct http-connection (url headers data))
url : string? headers : hasheq data : hasheq
struct
(struct http-response (request code headers body))
request : http-request? code : number? headers : hasheq body : hasheq
struct
(struct http-request (url method headers data))
url : string? method : symbol? headers : hasheq data : hasheq
procedure
(http-get conn [ #:data data #:path path #:headers headers]) → http-resonse? conn : (or/c string? http-connection?) data : hasheq = (hasheq) path : string? = "" headers : hasheq = (hasheq)
value
http-post : http-respone?
value
http-head : http-respone?
value
http-options : http-respone?
value
http-put : http-respone?
value
http-delete : http-respone?
value
http-patch : http-respone?
procedure
(http-do method conn [ #:data data #:path path #:headers headers]) → http-response? method : symbol? conn : http-connection? data : hasheq = (hasheq) path : string? = "" headers : hasheq = (hasheq)
3 Bug Report
Please go to github and create an issue for this repo.
4 TODOs
global param of debug mode to show request and response log msg just like the ruby faraday.
define a global param for pretty-print-depth for write-proc to show customized depth.
make param of hasheq can also be alist and dict data.