HTTP
The Http class in std/http provides standard, cross-platform methods to perform synchronous HTTP requests (GET, POST, DELETE) and concurrent HTTP fetches. All methods block execution and return a structured HttpResponse object.
Cross-Platform & Memory Protection
- Cross-Platform Compatibility: Runs natively on Desktop (using
libcurl) and inside browsers/WebAssembly (using Emscripten's Fetch API). - Memory Safety (OOM Protection): On Desktop, response data is restricted to a maximum of 5MB. If a response exceeds this limit, the VM terminates the transfer immediately to prevent heap exhaustion.
HttpResponse
All HTTP methods return an HttpResponse object containing the full response metadata and body.
class HttpResponse {
val status: Int // HTTP status code (200, 404, 500, ...)
val ok: Bool // true if status is in 200-299 range
val url: String // Final URL after redirects
val headers: Map<String, String> // Response headers
val body: String // Response body as string
}HTTP Methods
Make standard HTTP requests using Http.get, Http.post, and Http.delete. These calls block execution synchronously until the server responds or the timeout is reached.
@import("std/http")
val targetUrl = "https://api.github.com/repos/hoansdz/Autolang"
val timeoutMs = 5000 // 5 seconds timeout
try {
// 1. GET Request
val res = Http.get(targetUrl, timeoutMs)
println("Status: ${res.status}")
println("OK: ${res.ok}")
println("Body size: ${res.body.size()}")
// 2. POST Request
val postRes = Http.post("https://jsonplaceholder.typicode.com/posts", """{"title": "test", "body": "hello", "userId": 1}""", timeoutMs)
println("POST status: ${postRes.status}")
// 3. DELETE Request
val delRes = Http.delete("https://jsonplaceholder.typicode.com/posts/1", timeoutMs)
println("DELETE status: ${delRes.status}")
} catch (e) {
println("HTTP request failed: " + e.message)
}Concurrent Requests
Use Http.getAll to perform multiple GET requests in parallel. All requests run concurrently at the native host level and each result is returned as an HttpResponse in the same order as the input URLs.
@import("std/http")
val urls = <String>[
"https://jsonplaceholder.typicode.com/posts/1",
"https://jsonplaceholder.typicode.com/posts/2",
"https://jsonplaceholder.typicode.com/posts/3"
]
println("Starting concurrent fetches...")
val results = Http.getAll(urls, 5000)
for (res in results) {
if (res.ok) {
println("Success [${res.status}] size: ${res.body.size()}")
} else {
println("Failed: ${res.status}")
}
}Zero-Trust Security & Domain Filtering
The HTTP module implements strict URL verification before initiating network connections:
- URL Length Limit: URLs longer than 512 characters are immediately blocked to prevent buffer overflow and ReDoS attacks.
- Domain Whitelisting: The VM matches the extracted domain name against a regex defined by the host application. If no restriction is set, the VM defaults to blocking all external connections unless
allowAllDomainsis explicitly enabled.
Security Exception
If a script tries to call an HTTP method on an unauthorized domain or sends a malformed URL, a SecurityError exception is thrown, halting execution unless caught:
Methods Reference
Static Methods
Http.get(url: String, timeoutMs: Int = 10000): HttpResponse
Sends a synchronous GET request. Returns anHttpResponsewith status, headers, and body.Http.post(url: String, body: String, timeoutMs: Int = 10000): HttpResponse
Sends a synchronous POST request with the payload inbody.Http.delete(url: String, timeoutMs: Int = 10000): HttpResponse
Sends a synchronous DELETE request to the target URL.Http.getAll(urls: Array<String>, timeoutMs: Int = 10000): Array<HttpResponse>
Performs concurrent GET requests. Returns an array matching each URL's index order asHttpResponse.
