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.
Import
Section titled “Import”use std::core::httpResponse Format
Section titled “Response Format”All request functions return Result<HttpResponse, string> where HttpResponse
is an object with the following fields:
| Field | Type | Description |
|---|---|---|
status | number | HTTP status code (e.g., 200, 404) |
headers | HashMap<string, string> | Response headers |
body | string | Response body as text |
ok | bool | true if status is in the 200-299 range |
GET / DELETE
Section titled “GET / DELETE”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) // 201Mirroring 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 }, {})?Options
Section titled “Options”The options parameter is required on every call (pass {} for defaults). It
is an object that supports:
| Key | Type | Description |
|---|---|---|
headers | HashMap<string, string> | Custom request headers |
timeout | number | Request timeout in milliseconds |
let resp = await http::get("https://api.example.com/data", { headers: { "Authorization": "Bearer token123" }, timeout: 5000})?Permissions
Section titled “Permissions”All HTTP functions require the NetConnect permission. Running in a sandboxed
context without this permission will produce a runtime error.