HTTP Client (std::http)
std::http exposes the client sub-module for synchronous HTTP communication
with external APIs. Calls return a (resp, err) pair: when err != nil a
network or protocol failure occurred; otherwise resp contains status, ok,
headers and body.
Note: all examples below make real network requests and are therefore marked as
| norunin the WASM sandbox. Run them locally withzolo run.
Simple GET
The most common operation: fetch a resource by URL.
GET example with error handling; the response provides status, ok and body.
// Feature: http.client.get — HTTP GET request
// When to use: consume REST APIs, download pages, healthchecks.
// Response: { status, headers, body, status_text, ok } or (nil, err) on failure.
//
// SKIP: this example performs a live HTTP request (httpbin.org).
// In offline / sandboxed environments the call surfaces an internal
// error from the runtime rather than a clean (nil, err) result, so
// the body of the function is shown only as a comment.
use std::http
use std::log
fn fetch_status(url: str) {
// let resp, err = http.client.get(url)
// if err != nil {
// log.error("failure: {err}")
// return nil
// }
// return resp
print("(would GET {url})")
return nil
}
let _ = fetch_status("http://httpbin.org/get")
// Expected response shape (when the request succeeds):
// resp.status -> 200
// resp.ok -> true
// resp.headers["content-type"] -> "application/json"
// resp.body -> raw response body string
Requires the Zolo CLI/host — open in the playground or run locally.
POST with JSON body
Send structured data by combining std::json to serialize the payload and
http.client.post to transmit it.
POST with a JSON payload and reading the echo returned by the server.
// Feature: http.client.post — POST with a JSON body
// When to use: create resources, send webhooks, talk to REST APIs.
//
// SKIP: this example performs a live HTTP request (httpbin.org).
// In offline / sandboxed environments the call surfaces an internal
// error from the runtime rather than a clean (nil, err) result, so
// the network call is shown only as a comment.
use std::http
use std::json
use std::log
let payload = json.encode(#{
user_id: 42,
action: "login",
ts: 1700000000,
})
let headers = #{
"Content-Type": "application/json",
Accept: "application/json",
}
let ct = headers["Content-Type"]
print("payload: {payload}")
print("Content-Type: {ct}")
// Signature: http.client.post(url, body, headers?)
// let resp, err = http.client.post("http://httpbin.org/post", payload, headers)
//
// if err != nil {
// log.error("error: {err}")
// } else {
// print(resp.status) // expected: 200
// // The httpbin server echoes the body back — try decoding it.
// let echo = json.decode(resp.body)
// print(echo["json"]["action"]) // expected: login
// }
Requires the Zolo CLI/host — open in the playground or run locally.
Custom headers
Bearer authentication, User-Agent and idempotency keys are passed as a
#{key: value} map in the headers argument (second parameter for GET, third for
POST/PUT). The put, delete and patch methods follow the same pattern.
Authorization and User-Agent headers; overview of PUT/DELETE/PATCH.
// Feature: http.client — custom headers in requests
// When to use: authentication (Bearer), custom User-Agent, Accept-Language, idempotency keys.
//
// SKIP: this example performs a live HTTP request (httpbin.org).
// In offline / sandboxed environments the call surfaces an internal
// error from the runtime rather than a clean (nil, err) result, so
// the network call is shown only as a comment.
use std::http
let headers = #{
Authorization: "Bearer secret-token",
"User-Agent": "zolo-client/0.1",
Accept: "application/json",
"X-Request-Id": "abc123",
}
let auth = headers["Authorization"]
let ua = headers["User-Agent"]
print("Authorization: {auth}")
print("User-Agent: {ua}")
// GET with headers (optional as the second argument).
// let resp, err = http.client.get("http://httpbin.org/headers", headers)
//
// if err == nil {
// print(resp.status) // expected: 200
// print(resp.body) // httpbin echoes the headers it received
// }
// PUT/DELETE/PATCH follow the same pattern:
// http.client.put(url, body, headers?)
// http.client.delete(url, headers?)
// http.client.patch(url, body, headers?)
Requires the Zolo CLI/host — open in the playground or run locally.
Challenge
Adapt the POST example to send a webhook and validate the HMAC signature using
std::hash.hmac (see the hash module).