magus v0.3.0 is out. See what's new
¶ View markdown source · ✎ Suggest an edit
3 min read

http

HTTP client with automatic retry on transient errors.

Naming convention: import the module under its bare name (import "http"), reach members with a backslash, and call methods in camelCase: http\someMethod.

Note

The examples below are reference-only. http performs real IO (filesystem, process, network, or environment access) that the in-browser playground's sandbox cannot provide, so it is not registered there and its examples have no Run button. Pure-compute modules such as strings and json run their examples live in the page.

Methods

get

Send a GET request; returns {status, body, headers}. opts (curl-style): fail, fail_with_body, fail_early (bool); retry (int), retry_delay, retry_max_time, timeout (seconds, default 30); retry_all_errors, retry_connrefused (bool).

Signature: http\get(url, [headers], [opts]) → HttpResponse · source

Parameter Type Optional Description
url string
headers map[string]string yes
opts map[string]any yes

Returns: map[string]any

Example:

import "std";
import "http";

final r = http\get("https://api.github.com/repos/egladman/magus");
std\print(r.status);
std\print(r.body.sub(0, 80) + "...");

post

Send a POST request with body; returns {status, body, headers}. opts (curl-style): fail, fail_with_body, fail_early (bool); retry (int), retry_delay, retry_max_time, timeout (seconds, default 30); retry_all_errors, retry_connrefused (bool).

Signature: http\post(url, body, [headers], [opts]) → HttpResponse · source

Parameter Type Optional Description
url string
body string
headers map[string]string yes
opts map[string]any yes

Returns: map[string]any

Example:

import "http";

// Post JSON with a curl-style opts map for retry behavior.
// Escape { and } as \{ \} so Buzz does not try to interpolate them.
final r = http\post(
    "https://httpbin.org/post",
    "\{\"target\":\"build\"\}",
    {"Content-Type": "application/json"},
    {"retry": 3, "timeout": 10},
);

request

Send an HTTP request; returns {status, body, headers}. opts (curl-style): fail, fail_with_body, fail_early (bool); retry (int), retry_delay, retry_max_time, timeout (seconds, default 30); retry_all_errors, retry_connrefused (bool).

Signature: http\request(method, url, [body], [headers], [opts]) → HttpResponse · source

Parameter Type Optional Description
method string
url string
body string yes
headers map[string]string yes
opts map[string]any yes

Returns: map[string]any

Example:

import "http";

// request lets you pick any method; useful for PUT/PATCH/DELETE.
final r = http\request(
    "PUT",
    "https://httpbin.org/put",
    "hello",
    { "Content-Type": "text/plain" },
    { "timeout": 10 },
);

server

Start a static file server in the background from an options map and return the bound port. opts keys: dir (string) serves a single directory; OR mounts (a map of URL-prefix -> dir, e.g. {"/": "docs/gen", "/console/": "console/gen"}) serves multiple roots where a request routes to the LONGEST matching prefix, so "/console/" wins over "/" for a /console/ path and the matched prefix is stripped before the file lookup. Exactly one of dir or mounts is required. port (int, optional) binds that port; 0 (the default) scans upward from 8080 and binds the first available one. Unknown keys are rejected. Serves localhost only and runs until the process exits, so pair it with a blocking call like fs.watch.

Signature: http\server(opts) → int · source

Parameter Type Optional Description
opts map[string]any

Returns: int

Example:

import "http";

// Serve the current build output over http on port 8080 for quick sharing.
// Blocks until the process exits.
http\server({"dir": "dist/", "port": 8080});
auto-generatedhttpmodulestdlibmagusfile
Last updated (a170f9b2)
Earlier changes on this page (4)

Full history ↗ · Blame source ↗

Glossary

Magusfile

The magusfile.buzz that declares a project's targets (as export funs) and binds its spells. See targets.

Target

A named operation (build, test, ...) you invoke with magus run <target>; it may compose a spell's tool-native operations and depend on other targets. See targets.

Ward

A coded diagnostic that inspects a resolved op and nudges or blocks an anti-pattern before it runs. See wards.

Module

A magus stdlib namespace a magusfile imports for host capabilities: filesystem, exec, vcs, and more. See the module reference.

Buzz

The language magusfiles are written in (the .buzz engine). See engines.

Sandbox

The restricted filesystem and environment a target runs in, so builds stay reproducible and side-effect-free. See sandbox.

Conventions

Admonitions

Call-outs are rendered from GitHub-style alert blockquotes and carry a colored accent per type:

Note

Context worth knowing, but not a warning.

Warning

Something that can bite you if ignored.

The types are NOTE, TIP, IMPORTANT, WARNING, and CAUTION.