Skip to content

http

The http module provides an async HTTP client for making web requests. All functions are async and require the NetConnect permission. Under the hood, the module uses reqwest for robust HTTP handling.

use std::core::http

All request functions return Result<HttpResponse, string> where HttpResponse is an object with the following fields:

FieldTypeDescription
statusnumberHTTP status code (e.g., 200, 404)
headersHashMap<string, string>Response headers
bodystringResponse body as text
okbooltrue if status is in the 200-299 range

http::get(url, options) -> Result<HttpResponse, string>

Section titled “http::get(url, options) -> Result<HttpResponse, string>”

Perform an HTTP GET request. Pass {} for options to use defaults.

let resp = await http::get("https://api.example.com/data", {})?
if resp.ok {
print(resp.body)
}

http::delete(url, options) -> Result<HttpResponse, string>

Section titled “http::delete(url, options) -> Result<HttpResponse, string>”

Perform an HTTP DELETE request. Pass {} for options to use defaults.

let resp = await http::delete("https://api.example.com/items/1", {})?

Unlike a single polymorphic http::post, the HTTP module exposes three POST variants. Each sets a different Content-Type header automatically.

http::post_text(url, body: string, options) -> Result<HttpResponse, string>

Section titled “http::post_text(url, body: string, options) -> Result<HttpResponse, string>”

POST a text body. Sets Content-Type: text/plain; charset=utf-8. The string is sent verbatim. Pass {} for options to use defaults.

let resp = await http::post_text("https://api.example.com/notes", "hello world", {})?

http::post_bytes(url, body: Array<int>, options) -> Result<HttpResponse, string>

Section titled “http::post_bytes(url, body: Array<int>, options) -> Result<HttpResponse, string>”

POST a binary body. Sets Content-Type: application/octet-stream. Each element of body is a byte in 0..=255. Pass {} for options to use defaults.

let resp = await http::post_bytes("https://api.example.com/upload", [0x48, 0x69], {})?

http::post_json(url, body, options) -> Result<HttpResponse, string>

Section titled “http::post_json(url, body, options) -> Result<HttpResponse, string>”

POST a JSON body. Sets Content-Type: application/json. The body object is serialized to JSON before sending. Pass {} for options to use defaults.

let resp = await http::post_json(
"https://api.example.com/items",
{ name: "widget", price: 9.99 },
{}
)?
print(resp.status) // 201

Mirroring POST, PUT has three variants.

http::put_text(url, body: string, options) -> Result<HttpResponse, string>

Section titled “http::put_text(url, body: string, options) -> Result<HttpResponse, string>”

PUT a text body. Sets Content-Type: text/plain; charset=utf-8. Pass {} for options to use defaults.

let resp = await http::put_text("https://api.example.com/notes/1", "updated text", {})?

http::put_bytes(url, body: Array<int>, options) -> Result<HttpResponse, string>

Section titled “http::put_bytes(url, body: Array<int>, options) -> Result<HttpResponse, string>”

PUT a binary body. Sets Content-Type: application/octet-stream. Pass {} for options to use defaults.

let resp = await http::put_bytes("https://api.example.com/files/1", [0x48, 0x69], {})?

http::put_json(url, body, options) -> Result<HttpResponse, string>

Section titled “http::put_json(url, body, options) -> Result<HttpResponse, string>”

PUT a JSON body. Sets Content-Type: application/json. Pass {} for options to use defaults.

let resp = await http::put_json(
"https://api.example.com/items/1",
{ name: "updated widget", price: 12.99 },
{}
)?

The options parameter is required on every call (pass {} for defaults). It is an object that supports:

KeyTypeDescription
headersHashMap<string, string>Custom request headers
timeoutnumberRequest timeout in milliseconds
let resp = await http::get("https://api.example.com/data", {
headers: { "Authorization": "Bearer token123" },
timeout: 5000
})?

All HTTP functions require the NetConnect permission. Running in a sandboxed context without this permission will produce a runtime error.