Skip to content

HTTP Client — GET

The std::http module exposes http.get(url, headers?), which returns an object {status, ok, headers, body}. Zolo's error convention applies: on failure the function returns nil, message — it never throws an exception.

The example below shows how to build the headers map with the #{} literal and access the response fields. Because it requires real network access, the call appears commented out inside the file; the print line runs normally to document the expected shape:

http.get and the response structure {status, ok, headers, body}.

01-client-get.zolo
// Feature: HTTP client — simple GET
// Syntax: `http.get(url, headers?)` returns `{status, headers, body, ok}`
// When to use: consuming HTTP APIs, light scraping, healthchecks.
//
// SKIP: live network call disabled — runs only in environments with
// outbound HTTP access. The shape below documents the response.

use std::http

// On error returns `nil, msg` — Zolo's standard pattern for fallible APIs.
//
// let resp, err = http.get("http://httpbin.org/get")
// if resp == nil {
//   print("failed: {err}")
// } else {
//   print("status: {resp.status}")          // 200
//   print("ok? {resp.ok}")                   // true
//   let preview = resp.body.sub(1, 80)
//   print("first 80 chars: {preview}")
// }

print("(http.get is documented above; live request skipped)")

Requires the Zolo CLI/host — open in the playground or run locally.

To pass custom headers — Authorization, User-Agent, Accept — simply provide a map as the second argument:

Building the headers map and accessing individual fields.

02-client-headers.zolo
// Feature: HTTP client — GET with custom headers
// Syntax: `http.get(url, #{"Header-Name": "value"})`
// When to use: APIs that require `Authorization`, a specific `User-Agent`,
// or `Accept` for content negotiation.
//
// SKIP: live network call disabled — runs only in environments with
// outbound HTTP access. The shape below documents the response.

use std::http

let headers = #{
  "User-Agent": "zolo-lang/0.1",
  Accept: "application/json",
  Authorization: "Bearer fake-token-here",
}

let ua   = headers["User-Agent"]
let auth = headers["Authorization"]
print("User-Agent:    {ua}")
print("Authorization: {auth}")

// let resp, err = http.get("http://httpbin.org/headers", headers)
// if resp != nil {
//   print("status: {resp.status}")            // 200
//   print("body len: {resp.body.len()}")      // headers echoed back
// } else {
//   print("failed: {err}")
// }

Requires the Zolo CLI/host — open in the playground or run locally.

Challenge

Add an X-Request-Id header with a random value generated by std::math.random() and print it before sending the request.

enespt-br