Racket: How to do HTTP requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require | |
net/http-easy) | |
(define (base-url path) | |
(string-append "https://httpbin.org" path)) | |
; request: GET | |
; response: plain string body | |
(response-body | |
(get (base-url "/html"))) | |
; request: GET | |
; response: json body | |
(response-json | |
(get (base-url "/json"))) | |
; request: GET | |
; response: ignore body, take headers | |
(response-headers | |
(get (base-url "/json"))) | |
; request: GET auth bearer request header | |
; response: ignore body, validate http status code, take set-cookie response header | |
(let ([res (get (base-url "/bearer") | |
#:auth (bearer-auth "secret"))]) | |
(when (not (= 200 (response-status-code res))) | |
(error "not 200!")) | |
(response-headers-ref res 'server)) | |
; request: POST form fields body | |
; response: ignore body, validate http status code | |
(response-status-code | |
(post (base-url "/post") | |
#:form '((k1 . "v1") | |
(k2 . "v2")))) | |
; request: POST json body | |
; response: json body | |
(response-json | |
(post (base-url "/post") | |
#:json (hasheq 'foo "bar"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require | |
racket/port | |
net/uri-codec | |
net/url | |
net/url-connect | |
http/head | |
json) | |
(define (base-url path) | |
(string->url (string-append "https://httpbin.org" path))) | |
(define (heads-string->dict-downcase heads-string) | |
(for/hash ([(k v) (heads-string->dict heads-string)]) | |
(values (string->symbol (string-downcase (symbol->string k))) v))) | |
; Don't ignore (a potentially) invalid tls certificate | |
(current-https-protocol 'secure) | |
; request: GET | |
; response: plain string body | |
(call/input-url | |
(base-url "/html") | |
get-pure-port | |
port->string) | |
; request: GET | |
; response: json body | |
(call/input-url | |
(base-url "/json") | |
get-pure-port | |
read-json) | |
; request: GET | |
; response: ignore body, take headers | |
(call/input-url | |
(base-url "/json") | |
get-impure-port | |
(λ (in) (heads-string->dict-downcase (purify-port in)))) | |
; request: GET auth bearer request header | |
; response: ignore body, validate http status code, take set-cookie response header | |
(call/input-url | |
(base-url "/bearer") | |
(λ (url) (get-impure-port | |
url | |
'("authorization: Bearer secret"))) | |
(λ (in) | |
(let ([heads-string (purify-port in)]) | |
(when (not (= 200 (extract-http-code heads-string))) | |
(error "not 200!")) | |
(hash-ref (heads-string->dict-downcase heads-string) 'server)))) | |
; request: POST form fields body | |
; response: ignore body, validate http status code | |
(call/input-url | |
(base-url "/post") | |
(λ (url) (post-impure-port | |
url | |
(string->bytes/utf-8 (alist->form-urlencoded | |
(list (cons 'k1 "v1") (cons 'k2 "v2")))) | |
'("content-type: application/x-www-form-urlencoded; charset=utf-8"))) | |
(compose extract-http-code purify-port)) | |
; request: POST json body | |
; response: json body | |
(call/input-url | |
(base-url "/post") | |
(λ (url) (post-pure-port | |
url | |
#"{\"foo\":\"bar\"}" | |
'("content-type: application/json; charset=utf-8"))) | |
read-json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment