Module http/client
http/client
Async HTTP client with high-level fetch API.
Types
HttpError
enum
HttpError
HTTP client error variants.
Variants
| Variant | Fields | Description |
|---|---|---|
ConnectionFailed | msg: String | Failed to connect to the remote host. |
InvalidUrl | msg: String | The URL could not be parsed. |
Timeout | The request timed out. | |
TooManyRedirects | Too many HTTP redirects. | |
UnsupportedScheme | scheme: String | The URL scheme is not supported (only http/https). |
ResponseTooLarge | The response body exceeds the size limit. | |
Other | msg: 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
| Name | Type | Description |
|---|---|---|
method | HttpMethod | |
headers | ArrayList(HttpHeader) | |
body | String |
impl(FetchOptions, ...)
new : (FetchOptions) fn() -> FetchOptionsCreate default options (GET, no headers, empty body).
Returns: FetchOptions
with_method : (FetchOptions) fn(self : FetchOptions, method : HttpMethod) -> FetchOptionsSet the HTTP method.
Parameters
| Name | Type | Notes |
|---|---|---|
self | FetchOptions | |
method | HttpMethod |
Returns: FetchOptions
with_header : (FetchOptions) fn(self : FetchOptions, name : String, value : String) -> FetchOptionsAdd a request header.
Parameters
| Name | Type | Notes |
|---|---|---|
self | FetchOptions | |
name | String | |
value | String |
Returns: FetchOptions
with_body : (FetchOptions) fn(self : FetchOptions, body : String) -> FetchOptionsFunctions
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
| Name | Type | Notes |
|---|---|---|
url_str | String | |
opts | FetchOptions |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, 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
| Name | Type | Notes |
|---|---|---|
url_str | String |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Returns: Impl(Future(HttpResponse, IO, Exception))