Tanguv0.35.0
Web & HTTP15 exports
Raw Markdown

/http/client

Async and sync HTTP/HTTPS client with connection pooling, headers, timeouts, and streaming bodies.

(use /http/client)

Functions & symbols#

Client#

constructor
(Client [options]) -> Client

Constructs a reusable pooled HTTP/HTTPS client bound to `*runtime*`.

ParameterTypeOptionalDescription
optionsMapoptionalOptional client config {:timeout-ms :user-agent :follow-redirects}
Returns:Any
(let client (http/Client {:timeout-ms 10000}))

close-stream#

function
(close-stream stream) -> None

Closes an open SSE `Event-Stream` and cancels its underlying HTTP response stream.

ParameterTypeOptionalDescription
streamEvent-StreamrequiredOpen SSE stream handle
Returns:Any
(http/close-stream stream)

content-type-of#

function
(content-type-of msg [default]) -> (Or String None)

Returns the `Content-Type` header from an HTTP request or response Map.

ParameterTypeOptionalDescription
msgMaprequiredHTTP request or response Map
defaultAnyoptionalOptional default
Returns:Any
(http/content-type-of resp)

delete#

function
(delete client url [headers]) -> Map

Performs an HTTP `DELETE` request returning a response Map {:status :headers :body}.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client created with Client
urlStringrequiredTarget HTTP or HTTPS URL
headersMapoptionalOptional request headers map
Returns:Any
(let resp (http/delete client "https://api.example.com/items"))

get#

function
(get client url [headers]) -> Map

Performs an HTTP `GET` request returning a response Map {:status :headers :body}.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client created with Client
urlStringrequiredTarget HTTP or HTTPS URL
headersMapoptionalOptional request headers map
Returns:Any
(let resp (http/get client "https://api.example.com/items"))

get-text#

function
(get-text client url [headers]) -> String

Performs a `GET` request and returns the response body decoded directly as UTF-8 text.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client
urlStringrequiredTarget URL
headersMapoptionalOptional headers
Returns:Any
(let body (http/get-text client "https://example.com"))

header-of#

function
(header-of msg name [default]) -> (Or String None)

Looks up an HTTP header case-insensitively on a request or response Map.

ParameterTypeOptionalDescription
msgMaprequiredHTTP request or response Map
nameString | KeySymbolrequiredCase-insensitive header name
defaultAnyoptionalOptional default if absent
Returns:Any
(http/header-of resp :content-type)

options#

function
(options client url [headers]) -> Map

Performs an HTTP `OPTIONS` request returning a response Map {:status :headers :body}.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client created with Client
urlStringrequiredTarget HTTP or HTTPS URL
headersMapoptionalOptional request headers map
Returns:Any
(let resp (http/options client "https://api.example.com/items"))

patch#

function
(patch client url body [headers]) -> Map

Performs an HTTP `PATCH` request with `body` and returns a response Map {:status :headers :body}.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client created with Client
urlStringrequiredTarget HTTP or HTTPS URL
bodyString | Slab | MaprequiredRequest body payload
headersMapoptionalOptional request headers map
Returns:Any
(let resp (http/patch client "https://api.example.com/items" payload {:content-type "application/json"}))

post#

function
(post client url body [headers]) -> Map

Performs an HTTP `POST` request with `body` and returns a response Map {:status :headers :body}.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client created with Client
urlStringrequiredTarget HTTP or HTTPS URL
bodyString | Slab | MaprequiredRequest body payload
headersMapoptionalOptional request headers map
Returns:Any
(let resp (http/post client "https://api.example.com/items" payload {:content-type "application/json"}))

put#

function
(put client url body [headers]) -> Map

Performs an HTTP `PUT` request with `body` and returns a response Map {:status :headers :body}.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client created with Client
urlStringrequiredTarget HTTP or HTTPS URL
bodyString | Slab | MaprequiredRequest body payload
headersMapoptionalOptional request headers map
Returns:Any
(let resp (http/put client "https://api.example.com/items" payload {:content-type "application/json"}))

receive-event#

function
(receive-event stream) -> (Or Map None)

Reads the next parsed Server-Sent Event `{:event :data :id :retry}`, or `()` when the stream closes.

ParameterTypeOptionalDescription
streamEvent-StreamrequiredOpen SSE stream handle
Returns:Any
(while (let ev (http/receive-event stream)) (say ev:data))

request#

function
(request client req-map) -> Map

Executes an arbitrary HTTP request described by `req-map` and returns `{:status :headers :body}`.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client
reqMaprequiredRequest spec {:method :url [:headers :body]}
Returns:Any
(http/request client {:method "GET" :url "https://example.com"})

request-sse#

function
(request-sse client req-map) -> Event-Stream

Opens a Server-Sent Events (SSE) HTTP stream handle for incremental reading via `receive-event`.

ParameterTypeOptionalDescription
clientClientrequiredHTTP client
reqMaprequiredRequest spec {:method :url [:headers :body]}
Returns:Any
(let stream (http/request-sse client {:method "GET" :url "https://api.example.com/events"}))