Module http/client

http/client

Async HTTP client with high-level fetch API.

Types

HttpError enum
HttpError

HTTP client error variants.

Variants

VariantFieldsDescription
ConnectionFailedmsg: String

Failed to connect to the remote host.

InvalidUrlmsg: String

The URL could not be parsed.

Timeout

The request timed out.

TooManyRedirects

Too many HTTP redirects.

UnsupportedSchemescheme: String

The URL scheme is not supported (only http/https).

ResponseTooLarge

The response body exceeds the size limit.

Othermsg: String

An unclassified HTTP error.

Trait Implementations

impl(HttpError, ToString(...))
to_string : (self -> match(self, .ConnectionFailed(msg) => `HTTP connection failed: ${msg}`, .InvalidUrl(msg) => `Invalid URL: ${msg}`, .Timeout => `HTTP request timed out`, .TooManyRedirects => `Too many redirects`, .UnsupportedScheme(scheme) => `Unsupported scheme: ${scheme}`, .ResponseTooLarge => `Response too large`, .Other(msg) => `HTTP error: ${msg}` ) )
impl(HttpError, Error())
FetchOptions object
FetchOptions

Options for configuring an HTTP request.

Fields

NameTypeDescription
methodHttpMethod
headersArrayList(HttpHeader)
bodyString
impl(FetchOptions, ...)
new : (FetchOptions) fn() -> FetchOptions

Create default options (GET, no headers, empty body).

Returns: FetchOptions

with_method : (FetchOptions) fn(self : FetchOptions, method : HttpMethod) -> FetchOptions

Set the HTTP method.

Parameters

NameTypeNotes
selfFetchOptions
methodHttpMethod

Returns: FetchOptions

with_header : (FetchOptions) fn(self : FetchOptions, name : String, value : String) -> FetchOptions

Add a request header.

Parameters

NameTypeNotes
selfFetchOptions
nameString
valueString

Returns: FetchOptions

with_body : (FetchOptions) fn(self : FetchOptions, body : String) -> FetchOptions

Set the request body.

Parameters

NameTypeNotes
selfFetchOptions
bodyString

Returns: FetchOptions

Functions

fetch_with function
fn(url_str : String, opts : FetchOptions, using(io : IO)) -> Impl(Future(HttpResponse, IO, Exception))

Perform an HTTP request with custom options.

Example

opts := FetchOptions.new().with_method(.POST).with_body(`{"key": "value"}`);
resp := io.await(fetch_with(`http://example.com/api`, opts, using(io)));

Parameters

NameTypeNotes
url_strString
optsFetchOptions

Effects

NameTypeNotes
ioIOcomptime, implicit

Returns: Impl(Future(HttpResponse, IO, Exception))

fetch function
fn(url_str : String, using(io : IO)) -> Impl(Future(HttpResponse, IO, Exception))

Perform an HTTP GET request to the given URL string. Returns the HttpResponse on success.

Example

resp := io.await(fetch(`http://example.com`, using(io)));
cond(resp.is_ok() => println(resp.body), true => println(`Error`));

Parameters

NameTypeNotes
url_strString

Effects

NameTypeNotes
ioIOcomptime, implicit

Returns: Impl(Future(HttpResponse, IO, Exception))