@std/net/fetch.ms#
Import each required symbol with import Name from "@std/net/fetch.ms".
FetchBody Enum#
The request body: empty, text, raw bytes, or JSON text; fetch derives a default
Content-Type from the case.
enum FetchBody {
Empty
Text(value String)
Raw(value Bytes)
JsonText(value String)
}FetchBody.Empty#
Sends no body and no default Content-Type.
EmptyFetchBody.JsonText#
JSON text produced by json_body.
JsonText(value String)FetchBody.Raw#
Raw bytes, sent as application/octet-stream unless a Content-Type is given.
Raw(value Bytes)FetchBody.Text#
UTF-8 text, sent as text/plain; charset=utf-8 unless a Content-Type is given.
Text(value String)FetchBody.JsonText.value#
The encoded JSON text, sent as UTF-8 bytes.
value StringFetchBody.Raw.value#
The bytes, sent unchanged; the value is cloned per request.
value BytesFetchBody.Text.value#
The text, sent as UTF-8 bytes.
value StringFetchOptions Type#
Per-request settings. Every field has a default, so FetchOptions() (the default) is
a plain GET with a 30 second deadline and a 16 MiB response cap; set only the fields
that differ, as in FetchOptions(method: HttpMethod.Post).
type FetchOptions {
method HttpMethod = HttpMethod.Get
headers HttpHeaders? = null
body FetchBody = FetchBody.Empty
timeout Duration? = seconds(30)
max_response_bytes Int? = 16777216
}FetchOptions.body#
The request body; defaults to Empty. A Text, Raw, or JSON body adds a
Content-Type only when headers lacks one.
body FetchBody = FetchBody.EmptyFetchOptions.headers#
Extra request headers; null sends no extra fields. The caller’s headers are never modified.
headers HttpHeaders? = nullFetchOptions.max_response_bytes#
Largest accepted response body in bytes; null sets no cap. Larger bodies return
EOVERFLOW.
max_response_bytes Int? = 16777216FetchOptions.method#
The request method; defaults to Get. Extension tokens use the Custom case.
method HttpMethod = HttpMethod.GetFetchOptions.timeout#
Whole-exchange deadline; null waits without limit.
timeout Duration? = seconds(30)FetchResponse Type#
A buffered response. Error statuses are ordinary responses; use check_status to
reject them.
type FetchResponse {
head HttpResponseMetadata
body Bytes
}FetchResponse.body#
The complete body, within the request’s max_response_bytes.
body BytesFetchResponse.head#
Status, headers, and protocol.
head HttpResponseMetadataFetchResponse.check_status#
Succeeds when the status is 200 through 299. Otherwise returns an Error of kind
EIO whose details hold the status, so response.check_status()? rejects error
responses.
fn check_status() Result[Void, Error]FetchResponse.json#
Decodes the body as JSON into T, as in response.json[User]()?; use
JsonValue for untyped data.
fn json[T Data](ignore_unknown_fields Bool = true) Result[T, Error]FetchResponse.ok#
Returns whether the status is 200 through 299.
fn ok() BoolFetchResponse.text#
Decodes the body as UTF-8.
fn text() Result[String, Error]Fetcher Type#
A reusable client with connection pooling. Bind it to a local and its scope closes
it, or call close yourself.
opaque type Fetcher {
}Fetcher.close#
Cancels in-flight requests and drops pooled connections; later calls return
EBADF.
move fn close() Result[Void, Error]Fetcher.fetch#
Starts the request and returns a promise of the response, whose body is buffered.
A Raw or Text body is resent on redirects that keep it. Requests started
together are in flight together.
fn fetch(url String, options FetchOptions = FetchOptions()) Promise[FetchResponse]Fetcher.stream#
Sends the request and returns the response with its body as a stream. The default
options.timeout is a whole-exchange deadline, so set it to null for long streams.
fn stream(url String, options FetchOptions = FetchOptions()) Result[StreamingResponse, Error]FetcherOptions Type#
Settings fixed when a Fetcher is created.
type FetcherOptions {
max_redirects Int? = 20
decompress Bool = true
cookies Bool = false
tls TlsOptions = TlsOptions(
server_name: "",
roots_pem: "",
certificate_pem: "",
private_key_pem: "",
alpn: [],
require_client_certificate: false,
)
}FetcherOptions.cookies#
Keep an in-memory cookie jar for the fetcher’s lifetime.
cookies Bool = falseFetcherOptions.decompress#
Decode gzip responses, bounded by max_response_bytes.
decompress Bool = trueFetcherOptions.max_redirects#
Most redirects followed per request; 0 returns the redirect itself and null sets no cap.
max_redirects Int? = 20FetcherOptions.tls#
TLS trust and client-certificate settings; add h2 to alpn to allow HTTP/2.
tls TlsOptions = TlsOptions(
server_name: "",
roots_pem: "",
certificate_pem: "",
private_key_pem: "",
alpn: [],
require_client_certificate: false,
)fetch Function#
Starts one buffered request on a one-shot client and returns its promise; requests
started together are in flight together. By default it follows up to 20 redirects,
limits the body to 16 MiB, and limits the exchange to 30 seconds. Set
FetchOptions fields to change the body and time limits; for the redirect limit
and other client settings, use a Fetcher.
fn fetch(url String, options FetchOptions = FetchOptions()) Promise[FetchResponse] { ... }fetcher Function#
Creates a reusable fetcher on net, whose restrictions apply to every request and
redirect; null uses the host network.
fn fetcher(options FetcherOptions = FetcherOptions(), net Network? = null) Result[Fetcher, Error] { ... }json_body Function#
Encodes value as a JSON request body, sent as application/json unless a
Content-Type is given.
fn json_body[T Data](value T) Result[FetchBody, Error] { ... }