network
The network namespace provides raw HTTP request capabilities for communicating with web APIs.
Asynchronous vs. Synchronous Requests
Section titled “Asynchronous vs. Synchronous Requests”When a callback function is provided:
- The request is dispatched non-blockingly and polled once per frame.
- The callback receives
body: string | nilwhen the response lands. - In-flight requests are automatically discarded if the script is unloaded before response arrival.
Functions
Section titled “Functions”network.get
Section titled “network.get”-- Synchronous (Blocking)local body = network.get(url: string, headers?: table) --> string | nil
-- Asynchronous (Non-Blocking)network.get(url: string, headers?: table, callback: function) --> nilnetwork.request
Section titled “network.request”-- Synchronous (Blocking)local body = network.request(method: string, url: string, options?: table) --> string | nil
-- Asynchronous (Non-Blocking)network.request(method: string, url: string, options?: table, callback: function) --> nilSupported HTTP methods: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS".
Options Table Structure
Section titled “Options Table Structure”| Field | Type | Description |
|---|---|---|
headers |
table |
Map of key-value header strings (e.g. { ["Authorization"] = "Bearer token" }). |
params |
table |
Form-encoded query parameters appended to URL. |
body |
string | table |
Raw payload string or table (form-encoded or JSON). |
content_type |
string |
Defaults to "application/x-www-form-urlencoded". Set to "application/json" for JSON. |
network_timeout |
number |
Inactivity timeout in seconds. |
absolute_timeout |
number |
Total request timeout in seconds (default: 1s sync, 15s async). |
Example: Asynchronous POST Request
Section titled “Example: Asynchronous POST Request”local payload = { user = "Player1", score = 1500}
network.request("POST", "https://api.example.com/v1/scores", { headers = { ["Authorization"] = "Bearer secret_api_token" }, content_type = "application/json", body = json.stringify(payload)}, function(body) if body then print("Score submitted successfully: " .. body) else print("Network request failed or timed out.") endend)