zena:fetch

zena
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 ​

zena
import { fetch } from 'zena:fetch';

let response = await fetch('https://example.com/api/data');
if (response.ok) {
  let text = await response.text();
}

Classes

Response

zena
resource class Response extends Resource
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.

Constructors
zena
new(wire: Own<WireResponse>)
#
Properties
zena
status: i32
#

The HTTP status code.

zena
ok: boolean { get; }
#

True for a 2xx status, exactly like the web's Response.ok.

Methods
zena
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.

zena
async text(): Future<String>
#

The response body, decoded as text. Consumes the response.

zena
async bytes(): Future<FixedArray<u8>>
#

The response body's bytes. Consumes the response.

Type aliases

RequestInit

zena
type RequestInit = {method?: String, headers?: Array<(String, String)>, body?: String | FixedArray<u8> | null}
Re-exported from zena:fetch/options.zena

Options for an HTTP request sent with fetch.

Functions

fetch

zena
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.