HTTP Client
1 Common Usage Example
1.1 Explicitly request URLs
1.2 Request nuance URLs
1.2.1 Define connections
1.2.2 Do the requests
2 Reference
2.1 Parameters
current-http-client/  user-agent
current-http-client/  response-auto
current-http-client/  pretty-print-depth
2.2 Structs
http-connection
http-response
http-request
2.3 Requests
http-get
http-post
http-head
http-options
http-put
http-delete
http-patch
http-do
3 Others
3.1 Bug Report
3.2 TODOs
3.3 Change Logs
8.0

HTTP Client

Yanying Wang <yanyingwang1@gmail.com>

A practical Racket HTTP client for interacting data with HTTP servers.

1 Common Usage Example

1.1 Explicitly request URLs

Request https://httpbin.org/anything/fruits?color=red&made-in=China&price=10 with setting request headers Token: your-token would be like below:
> (http-get "https://httpbin.org"
            #:path "anything/fruits"
            #:data (hasheq 'color "red" 'made-in "China" 'price 10)
            #:headers (hasheq 'Token "your-token"))

1.2 Request nuance URLs

You can define a http-connection, and use it to do requests with modifying some details of it.

1.2.1 Define connections

Predefine a http-connection with presetting url/path/headers/data:
> (define httpbin-org/anthing
      (http-connection "https://httpbin.org/anything"
                       (hasheq 'Content-Type "application/json")
                       (hasheq 'made-in "China" 'price 10)))

1.2.2 Do the requests

Do a GET request with adding path/data/headers to the predefined http-connection:

2 Reference

2.1 Parameters

parameter

(current-http-client/user-agent)  string?

(current-http-client/user-agent v)  void?
  v : string?
 = "http-client[your-system-name/your-vm-sytem-type-name/your-racket-version]"
The user agent name used by requesting the HTTP servers.

Added in version 1.0 of package http-client.

Set this parameter to #f to disable the auto convertion of the response’s body data. In another word, http-response-body of a http-response will be a raw string if set this parameter to #f.

Changed in version 1.0 of package http-client: renamed from current-http-response-auto

This parameter is used by displaying structs of http-connection/http-request/http-response, check pretty-print-depth for its implement details.

Added in version 1.0 of package http-client.

Examples:
> (define conn1
    (http-connection "https://httpbin.org/anything"
                     (hasheq 'Content-Type "application/json" 'Accept "application/json")
                     (hasheq 'made-in "China" 'price 10)))
> (current-http-client/pretty-print-depth)

1

> conn1

#<http-connection "https://httpbin.org/anything" headers: '#hasheq((...) (...)) data: '#hasheq((...) (...))>

> (current-http-client/pretty-print-depth 2)
> conn1

#<http-connection "https://httpbin.org/anything" headers: '#hasheq((Accept . "application/json") (Content-Type . "application/json")) data: '#hasheq((made-in . "China") (price . 10))>

2.2 Structs

The displaying of HTTP client strcuts is controlled by current-http-client/pretty-print-depth.

struct

(struct http-connection (url headers data))

  url : string?
  headers : hasheq
  data : hasheq
Construct a http-connection instance and use it later by such as http-get when you’re requesting same website with nuance path/data/headers, check Request nuance URLs for usage examples.

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 such as using http-get.

struct

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

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

2.3 Requests

procedure

(http-get conn    
  [#:path path    
  #:data data    
  #:headers headers])  http-response?
  conn : (or/c string? http-connection?)
  path : string? = ""
  data : hasheq = (hasheq)
  headers : hasheq = (hasheq)

procedure

(http-post conn    
  [#:path path    
  #:data data    
  #:headers headers])  http-response?
  conn : (or/c string? http-connection?)
  path : string? = ""
  data : hasheq = (hasheq)
  headers : hasheq = (hasheq)

procedure

(http-head conn    
  [#:path path    
  #:data data    
  #:headers headers])  http-response?
  conn : (or/c string? http-connection?)
  path : string? = ""
  data : hasheq = (hasheq)
  headers : hasheq = (hasheq)

procedure

(http-options conn    
  [#:path path    
  #:data data    
  #:headers headers])  http-response?
  conn : (or/c string? http-connection?)
  path : string? = ""
  data : hasheq = (hasheq)
  headers : hasheq = (hasheq)

procedure

(http-put conn    
  [#:path path    
  #:data data    
  #:headers headers])  http-response?
  conn : (or/c string? http-connection?)
  path : string? = ""
  data : hasheq = (hasheq)
  headers : hasheq = (hasheq)

procedure

(http-delete conn    
  [#:path path    
  #:data data    
  #:headers headers])  http-response?
  conn : (or/c string? http-connection?)
  path : string? = ""
  data : hasheq = (hasheq)
  headers : hasheq = (hasheq)

procedure

(http-patch conn    
  [#:path path    
  #:data data    
  #:headers headers])  http-response?
  conn : (or/c string? http-connection?)
  path : string? = ""
  data : hasheq = (hasheq)
  headers : hasheq = (hasheq)
Procedures to do the http requests.

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

3 Others

3.1 Bug Report

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

3.2 TODOs

3.3 Change Logs