zena:fetch
import {…} from 'zena:fetch';
HTTP client for making network requests.
fetch(url, init) sends an HTTP request and completes with a Response.
The response provides the HTTP status, headers, and methods for
asynchronously reading the response body.
Examples ​
import { fetch } from 'zena:fetch';
let response = await fetch('https://example.com/api/data');
if (response.ok) {
let text = await response.text();
}
Classes
Response
resource class Response extends Resource
An HTTP response: the status and headers, and the body, which stays on the host until it is read.
A resource class over the wire response, held as Own<Response>.
Reading the body consumes the response (text, bytes), so it is
read at most once, and a second read is a use after move. Read the
status and any headers you need first. A response dropped unread
releases the wire response, and with it the body the host holds.
new(wire: Own<WireResponse>)
header(name: String): String | null
The first value of the response header name, or null when the
response carries none. Header names are case-insensitive; values
are bytes on the wire and decoded here as text.
async text(): Future<String>
The response body, decoded as text. Consumes the response.
async bytes(): Future<FixedArray<u8>>
The response body's bytes. Consumes the response.
Type aliases
RequestInit
type RequestInit = {method?: String, headers?: Array<(String, String)>, body?: String | FixedArray<u8> | null}
Options for an HTTP request sent with fetch.
Functions
fetch
async function fetch(url: String | URL, init: RequestInit = {}): Future<Own<Response>>
Starts fetching url, returning a future that resolves once the
response is available.
init sets the request method (default 'GET'), headers, and body
(String or FixedArray<u8>). The future resolves to the Response
for the request: its status and headers are in, and its body is read
from it afterwards.
The future fails only when the request itself fails — a URL that
does not parse, a host the runtime could not resolve or reach, a
connection that closed before a response arrived. It does not fail
when the server answers with a status that means an error (404,
504, and so on): that is a response like any other, and the caller
checks ok or status.