Client
is an HTTP client that supports both HTTP and HTTPS.
Basic GET
(match (Client.get "https://example.com/")
(Result.Success r) (println* (Response.body &r))
(Result.Error e) (IO.errorln &e))
POST with headers
(match (Client.post "https://api.example.com/data"
{@"Content-Type" [@"application/json"]}
"{\"key\": 1}")
(Result.Success r) (println* (Response.body &r))
(Result.Error e) (IO.errorln &e))
Streaming
(match (Client.request-stream "POST" url headers body)
(Result.Success stream)
(do
(while-do true
(match (ResponseStream.poll &stream)
(Maybe.Nothing) (break)
(Maybe.Just chunk) (IO.print &chunk)))
(ResponseStream.close stream))
(Result.Error e) (IO.errorln &e))
default-max-redirects
Int
is the default maximum number of HTTP redirects
to follow. Used by request, request-stream, and convenience methods.
del
(Fn [(Ref String a)] (Result Response String))
(del url)
performs an HTTP DELETE request. Returns (Result Response String).
del-with-config
(Fn [(Ref String a), (Ref RequestConfig b)] (Result Response String))
(del-with-config url config)
performs an HTTP DELETE request with the given
RequestConfig. Returns (Result Response String).
See RequestConfig for timeout and redirect details.
del-with-jar
(Fn [(Ref String a), (Ref CookieJar b)] (Result Response String))
(del-with-jar url jar)
performs an HTTP DELETE request using the given CookieJar.
Returns (Result Response String).
get
(Fn [(Ref String a)] (Result Response String))
(get url)
performs an HTTP GET request. Returns (Result Response String).
get-with-config
(Fn [(Ref String a), (Ref RequestConfig b)] (Result Response String))
(get-with-config url config)
performs an HTTP GET request with the given
RequestConfig. Returns (Result Response String).
See RequestConfig for timeout and redirect details.
get-with-jar
(Fn [(Ref String a), (Ref CookieJar b)] (Result Response String))
(get-with-jar url jar)
performs an HTTP GET request using the given CookieJar.
Returns (Result Response String).
head
(Fn [(Ref String a)] (Result Response String))
(head url)
performs an HTTP HEAD request. Returns (Result Response String).
The response body will be empty, but headers and status code are available.
head-with-config
(Fn [(Ref String a), (Ref RequestConfig b)] (Result Response String))
(head-with-config url config)
performs an HTTP HEAD request with the given
RequestConfig. Returns (Result Response String).
See RequestConfig for timeout and redirect details.
head-with-jar
(Fn [(Ref String a), (Ref CookieJar b)] (Result Response String))
(head-with-jar url jar)
performs an HTTP HEAD request using the given CookieJar.
Returns (Result Response String).
patch
(Fn [(Ref String a), (Map String (Array String)), (Ref String b)] (Result Response String))
(patch url headers body)
performs an HTTP PATCH request with headers and body.
Returns (Result Response String).
patch-with-config
(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref RequestConfig c)] (Result Response String))
(patch-with-config url headers body config)
performs an HTTP PATCH request with the given RequestConfig.
Returns (Result Response String).
See RequestConfig for timeout and redirect details.
patch-with-jar
(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref CookieJar c)] (Result Response String))
(patch-with-jar url headers body jar)
performs an HTTP PATCH request using the given CookieJar.
Returns (Result Response String).
post
(Fn [(Ref String a), (Map String (Array String)), (Ref String b)] (Result Response String))
(post url headers body)
performs an HTTP POST request with headers and body.
Returns (Result Response String).
post-multipart
(Fn [(Ref String a), (Map String (Array String)), (Ref (Array Part) b)] (Result Response String))
(post-multipart url headers parts)
performs an HTTP POST request with a multipart/form-data
body. Generates a boundary, encodes the parts, and sets the Content-Type
header automatically. Any Content-Type header in headers is overwritten.
Returns (Result Response String).
(Client.post-multipart "https://example.com/upload"
{}
&[(Multipart.text-part "field" "value")
(Multipart.file-part "upload" "test.txt"
"text/plain" "file contents")])
post-multipart-with-config
(Fn [(Ref String a), (Map String (Array String)), (Ref (Array Part) b), (Ref RequestConfig c)] (Result Response String))
(post-multipart-with-config url headers parts config)
performs an HTTP POST request with a
multipart/form-data body using the given RequestConfig.
Returns (Result Response String).
See RequestConfig for timeout and redirect details.
post-with-config
(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref RequestConfig c)] (Result Response String))
(post-with-config url headers body config)
performs an HTTP POST request with the given
RequestConfig. Returns (Result Response String).
See RequestConfig for timeout and redirect details.
post-with-jar
(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref CookieJar c)] (Result Response String))
(post-with-jar url headers body jar)
performs an HTTP POST request using the given CookieJar.
Returns (Result Response String).
put
(Fn [(Ref String a), (Map String (Array String)), (Ref String b)] (Result Response String))
(put url headers body)
performs an HTTP PUT request with headers and body.
Returns (Result Response String).
put-with-config
(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref RequestConfig c)] (Result Response String))
(put-with-config url headers body config)
performs an HTTP PUT request with the given
RequestConfig. Returns (Result Response String).
See RequestConfig for timeout and redirect details.
put-with-jar
(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref CookieJar c)] (Result Response String))
(put-with-jar url headers body jar)
performs an HTTP PUT request using the given CookieJar.
Returns (Result Response String).
request
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c)] (Result Response String))
(request verb url headers body)
sends an HTTP request to the given URL. Returns (Result Response String).
Follows up to default-max-redirects HTTP redirects automatically. For 301,
302, and 303 responses a GET or HEAD keeps its method and any other method
becomes a GET. For 307 and 308 the original method is preserved.
See request-with-max-redirects to control the redirect limit.
request-stream
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c)] (Result ResponseStream String))
(request-stream verb url headers body)
sends an HTTP request and returns a ResponseStream for
reading the response body incrementally. Follows up to default-max-redirects
HTTP redirects automatically. Use ResponseStream.poll to read chunks.
The stream handles chunked transfer-encoding automatically. Check
ResponseStream.status-code to verify the request succeeded before polling.
See request-stream-with-max-redirects to control the redirect limit.
request-stream-with-config
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref RequestConfig d)] (Result ResponseStream String))
(request-stream-with-config verb url headers body config)
sends an HTTP request with the given
RequestConfig and returns a ResponseStream.
(let [cfg (RequestConfig.init 5 10 10)]
(Client.request-stream-with-config "POST" url headers body &cfg))
See RequestConfig for timeout and redirect details.
request-stream-with-jar
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d)] (Result ResponseStream String))
(request-stream-with-jar verb url headers body jar)
sends an HTTP request using the given
CookieJar. Matching cookies are sent automatically, and Set-Cookie
response headers are stored in the jar. Returns a ResponseStream.
Follows up to default-max-redirects redirects.
request-stream-with-jar-and-config
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d), (Ref RequestConfig e)] (Result ResponseStream String))
(request-stream-with-jar-and-config verb url headers body jar config)
sends an HTTP request using the
given CookieJar and RequestConfig. Returns a ResponseStream.
request-stream-with-max-redirects
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), Int] (Result ResponseStream String))
(request-stream-with-max-redirects verb url headers body max-redirects)
sends an HTTP request and returns a
ResponseStream, following up to max-redirects HTTP redirects.
For 301, 302, and 303 responses a GET or HEAD keeps its method; any other method becomes a GET, and the body and the headers describing it are dropped. For 307 and 308 responses the original method and body are preserved.
Pass 0 to disable redirect following.
request-with-config
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref RequestConfig d)] (Result Response String))
(request-with-config verb url headers body config)
sends an HTTP request with the given RequestConfig.
Returns (Result Response String).
(let [cfg (RequestConfig.init 5 10 10)]
(Client.request-with-config "GET" url headers body &cfg))
See RequestConfig for timeout and redirect details.
request-with-jar
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d)] (Result Response String))
(request-with-jar verb url headers body jar)
sends an HTTP request using the given CookieJar.
Matching cookies are sent automatically, and Set-Cookie response headers
are stored in the jar. Returns (Result Response String).
Follows up to default-max-redirects redirects.
request-with-jar-and-config
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d), (Ref RequestConfig e)] (Result Response String))
(request-with-jar-and-config verb url headers body jar config)
sends an HTTP request using the given
CookieJar and RequestConfig. Returns (Result Response String).
request-with-max-redirects
(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), Int] (Result Response String))
(request-with-max-redirects verb url headers body max-redirects)
sends an HTTP request, following up to max-redirects redirects.
Returns (Result Response String). Pass 0 to disable redirect following.