http-client
1 Example
2 Reference
current-http-user-agent
current-http-response-auto
http-connection
http-response
http-request
http-get
http-post
http-head
http-options
http-put
http-delete
http-patch
http-do
3 Bug Report
4 TODOs
7.8

http-client

Yanying Wang <yanyingwang1@gmail.com>

A practical http client library for sending data to http servers.

    1 Example

    2 Reference

    3 Bug Report

    4 TODOs

1 Example

Examples:
> (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

set the user agent which is used by the time of doing the request to http server.

set it to false to disable the auto convertion of the response’s body data.

struct

(struct http-connection (url headers data))

  url : string?
  headers : hasheq
  data : hasheq
construct a http-connection instance and use it with such as http-get when you want to request the same website with similar data.

struct

(struct http-response (request code headers body))

  request : http-request?
  code : number?
  headers : hasheq
  body : hasheq
You will get a http-response struct instance if you’re doing a request using this lib’s function such as http-get.

struct

(struct http-request (url method headers data))

  url : string?
  method : symbol?
  headers : hasheq
  data : hasheq
for the most common time, http-request is included in the http-response instance.

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)
request a website with get method.

value

http-post : http-respone?

same as http-get except of doing a not get method request.

value

http-head : http-respone?

same as http-get except of doing a not get method request.

value

http-options : http-respone?

same as http-get except of doing a not get method request.

value

http-put : http-respone?

same as http-get except of doing a not get method request.

value

http-delete : http-respone?

same as http-get except of doing a not get method request.

value

http-patch : http-respone?

same as http-get except of doing a not get method request.

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)
a low level function to do the http request.

3 Bug Report

Please go to github and create an issue for this repo.

4 TODOs