Skip to main content

Networking

lib/net/ provides HTTP and TCP primitives plus a JSON-over-HTTP convenience class. Most operations are async — use await to consume the resulting Promise<T>.

HTTP — Http (static facade)

Http is a static facade with both sync and async variants. Sync methods internally await the async natives.

Async

import * from "lib/net/Http.mt";

function async main(): Promise<void> {
HttpResponse resp = await Http::getAsync("https://api.example.com/users/42");
if (resp.isOk()) {
print(resp.body);
}
}
MethodReturns
Http::getAsync(string url)Promise<HttpResponse>
Http::postAsync(string url, string body)Promise<HttpResponse>
Http::putAsync(string url, string body)Promise<HttpResponse>
Http::deleteAsync(string url)Promise<HttpResponse>

Sync

The sync variants block (internally awaiting) and return HttpResponse directly:

HttpResponse resp = Http::get("https://api.example.com/users/42");
print(resp.status);
print(resp.body);
MethodReturns
Http::get(string url)HttpResponse
Http::post(string url, string body)HttpResponse
Http::put(string url, string body)HttpResponse
Http::delete(string url)HttpResponse

Builder Entry Point

For finer control over headers, method, and timeout, build an HttpRequest:

HttpRequest req = Http::request("GET", "https://api.example.com/data");
// configure req (headers, timeout, etc.) then send

HttpResponse

HttpResponse exposes:

  • int status — HTTP status code
  • string body — response body
  • isOk(): bool — true for 2xx
  • (plus headers via the standard accessor methods on the type)

JSON-over-HTTP — JsonApi (instance class)

JsonApi is a stateful HTTP client wrapper that defaults Accept: application/json and offers fluent header / timeout configuration. Construct one per base URL — its methods are instance methods.

import * from "lib/net/JsonApi.mt";

function async main(): Promise<void> {
JsonApi api = new JsonApi("https://api.example.com");
api.setHeader("Authorization", "Bearer abc123");
api.setTimeout(10000);

String body = await api.get("/users/42");
print(body.getValue());
}

Methods

MethodReturns
setHeader(string name, string value)JsonApi (fluent)
setTimeout(int ms)JsonApi (fluent)
async get(string path)Promise<String> (raw body)
async post(string path, string jsonBody)Promise<String>
async put(string path, string jsonBody)Promise<String>
async delete(string path)Promise<String>
async getAs<T>(string path, string className)Promise<T> (deserialized)
async postAs<T>(string path, T obj, string className)Promise<T>

Non-2xx responses raise HttpException carrying the status code.

Typed GET / POST

getAs<T> and postAs<T> combine HTTP with JSON deserialization in one call:

JsonApi api = new JsonApi("https://api.example.com");
User u = await api.<User>getAs("/users/42", "User");
print(u.name);

postAs serializes the request body, sends it, and deserializes the response — useful for typed CRUD APIs.

TCP

import * from "lib/net/TcpServer.mt";
import * from "lib/net/TcpSocket.mt";

function async main(): Promise<void> {
TcpServer server = new TcpServer();

server.onConnection(async client -> {
String data = await client.receiveAsync(1024);
await client.sendAsync("echo: " + data.getValue());
client.close();
});

server.listen(8080);
await delay(30000);
server.stop();
}

For a larger TCP sample, see examples/chat-room/, which starts a callback-based chat server and scripted clients.

Exceptions

lib/net/exceptions/ defines:

  • NetworkException
  • ConnectionException
  • DnsException
  • HttpException (carries status)
  • TimeoutException

See Exceptions for catching patterns.

See Also