Modules · Connectivity

Network

The HTTP client. A request does not block: it returns a Response value that starts out loading and stays that way until a driver fires the fetch and fills it in, so a view built from it shows a spinner until the data arrives and then re-renders on its own. Request and Post give you the raw status and body; the Json pair go further, so the driver handles the encoding and decoding for you. Json.Get parses the response into a reactive structure you read with ordinary field and array access, and Json.Post walks a reactive structure into the request body and holds the send until every value in it has settled. There is no JSON.stringify or JSON.parse in program code.

Contents

Functions

Functions

Network.Web.Client.Request

An HTTP GET that returns a loading text response, filled in when the fetch resolves.

Parameters

NameTypeDescription
urlanyThe URL to fetch; plain or reactive.
delayMsIntegerOptional. Holds the loading state for this many milliseconds before fetching; a test aid.

Returns

A Network.Web.Response state {status, body}, loading until the response arrives.

Example

const page = Network.Web.Client.Request("/hello.txt");
Content.Text(page.body);
Text that appears once the GET resolves

Network.Web.Client.Post

An HTTP POST of a prepared body string, returning a loading text response.

Parameters

NameTypeDescription
urlanyThe URL to post to; plain or reactive.
bodyanyA body string prepared by the caller, sent with Content-Type application/json.
delayMsIntegerOptional. Holds the loading state before fetching; a test aid.

Returns

A Network.Web.Response state {status, body}, loading until the response arrives.

Example

const saved = Network.Web.Client.Post("/save", '{"ok":true}');
Content.Text(saved.status);
Post a ready-made JSON string

Network.Web.Client.Json.Get

A GET whose JSON response is decoded into a reactive structure you read by field and array access.

Parameters

NameTypeDescription
urlanyThe URL to fetch; plain or reactive.
delayMsIntegerOptional. Holds the loading state before fetching; a test aid.

Returns

A Network.Web.Json.Response state {status, data}. The driver parses the JSON, so data is a reactive structure (arrays keyed by element index) read without JSON.parse.

Example

const user = Network.Web.Client.Json.Get("/user.json");
Content.Text(user.data.name);
Read a decoded field, no parsing

Network.Web.Client.Json.Post

A POST that encodes a reactive data structure to JSON automatically and decodes the JSON response.

Parameters

NameTypeDescription
urlanyThe URL to post to; plain or reactive.
dataanyA reactive structure (objects, arrays, values). The driver walks it, holds the request until every value has settled, then encodes it to JSON.
delayMsIntegerOptional. Holds the loading state before fetching; a test aid.

Returns

A Network.Web.Json.Response state {status, data}, decoded like Json.Get.

Example

const name = Reactive.State(Text)("Ada");
const saved = Network.Web.Client.Json.Post("/save", { name: name });
Content.Text(saved.data.id);
Send a reactive structure, read the decoded reply