HTTP client and server. import http, effect.
Start an HTTP server on the given port with a request handler
http.serve(3000, (req) => http.response(200, "ok"))
Create a plain text HTTP response with status code
http.response(200, "Hello!")
Create a JSON HTTP response with status code
http.json(200, json.stringify(data))
Create a response with custom headers
http.with_headers(200, body, {"Content-Type": "text/html"})
Create a 302 temporary redirect response
http.redirect("/new-path")
Set the status code on a response
http.status(resp, 201)
Get the body string from a response
let text = http.body(resp)
Set a header on a response
http.set_header(resp, "X-Custom", "value")
Get a header value from a response
let ct = http.get_header(resp, "Content-Type")
Get the HTTP method of a request (GET, POST, etc.)
let method = http.req_method(req)
Get the URL path of a request
let path = http.req_path(req)
Get the body string of a request
let body = http.req_body(req)
Get a header value from a request
let auth = http.req_header(req, "Authorization")
Get all query parameters from a request as a map. Values are percent-decoded
(%XX → byte, + → space), so ?q=%E7%8C%AB yields {"q": "猫"}.
let params = http.query_params(req) // {"page": "1", "q": "test"}
Percent-decode a URL component (%XX → byte, + → space). query_params
already decodes its values; use this for manually-extracted query/form text.
let q = http.url_decode("%E7%8C%AB") // "猫"
Send an HTTP GET request and return the response body
let html = http.get("https://example.com")
Send an HTTP POST request with a body string
let resp = http.post("https://api.example.com", body)
Send an HTTP PUT request
let resp = http.put(url, body)
Send an HTTP PATCH request
let resp = http.patch(url, body)
Send an HTTP DELETE request
let resp = http.delete(url)
http.request(method: String, url: String, body: String, headers: Map[String, String]) -> Result[String, String]
Send a custom HTTP request with method, URL, body, and headers
let resp = http.request("PUT", url, body, headers)
Send an HTTP GET and return the raw response body as Bytes (no UTF-8
conversion), so binary payloads such as images or TTS audio survive intact.
The String client corrupts non-UTF-8 bodies.
let audio = http.get_bytes("https://tts.example.com/say.mp3")!
fs.write_bytes_raw("say.mp3", audio)!
http.request_bytes(method: String, url: String, body: String, headers: Map[String, String]) -> Result[Bytes, String]
Like http.request but returns the raw response body as Bytes.
let blob = http.request_bytes("POST", url, body, headers)!