Skip to main content

HTTP Network Library (Http)

Http needs the http feature flag, which isn't part of this docs site's browser build — so every example below is reference only, not runnable here. That's specific to this build, though, not a limitation of Stof itself: wiring up Http.fetch from a TypeScript host is a small amount of code, since Stof's async model bridges naturally onto JS/browser Promises. Plenty of real Stof embeddings do have this library available — including the separate live playground.

A fetch runs on a background thread pool rather than blocking the calling process — which is the Async process model doing real work: Http.fetch spawns its own process, and awaiting it is what lets several requests actually run in parallel instead of one at a time.

Making Requests

async Http.fetch(url: str, method: str = "get", body: str | blob = null, headers: map = null, timeout: seconds = null, query: map = null, bearer: str = null) -> Promise<map>

Every other function on this page operates on the map this returns.

const resp = await Http.fetch("https://restcountries.com/v3.1/region/europe");

Reading the Response

Http.success(response: map) -> bool

Status in [200, 299].

const resp = await Http.fetch("https://restcountries.com/v3.1/region/europe");
assert(Http.success(resp));

Http.client_error(response: map) -> bool

Status in [400, 499].

assert_not(Http.client_error(resp));

Http.server_error(response: map) -> bool

Status in [500, 599].

assert_not(Http.server_error(resp));

Http.text(response: map) -> str

The response body as UTF-8 text — equivalent to Http.blob(response) as str.

const body = Http.text(resp);

Http.blob(response: map) -> blob

The response body as raw bytes.

const body = Http.blob(resp);

Http.size(response: map) -> bytes

The response body's size — a unit type, so casting it to something more readable is one step:

const mib_body_size = Http.size(resp) as MiB;

Http.parse(response: map, context: obj = self) -> obj

Parses the response body directly into an object, using the response's Content-Type header to pick the format — stof if that header is missing. Throws if the format isn't one this graph accepts, or if there's no body to parse:

const body = new {};
try { Http.parse(resp, body); }
catch { /* didn't work out */ }