http

HTTP client and server using raw POSIX sockets with optional BearSSL for HTTPS.

Import

import http

HTTP client

http.get(url: str, opts?: map) -> map

Send a GET request and return a response map with status, body, headers.

http.post(url: str, body?: str, opts?: map) -> map

Send a POST request with an optional body string.

http.put(url: str, body?: str, opts?: map) -> map

Send a PUT request.

http.delete(url: str, opts?: map) -> map

Send a DELETE request.

http.patch(url: str, body?: str, opts?: map) -> map

Send a PATCH request.

http.request(opts: map) -> map

Low-level request builder. opts may include method, url, headers, body.

HTTP server

http.serve(port: int, handler: fn | router)

Start an HTTP server on the given port. The handler receives a request map with method, path, query, headers, body, and must return a response map with at least status and body. Alternatively, pass a router map with a routes key for path-based dispatch.

Examples

import http

-- client: GET
let r = http.get("https://httpbin.org/get")
println(r["status"])  -- 200
println(r["body"])

-- client: POST with JSON body
import json
let body = json.stringify(#{"name": "Alice"})
let r2 = http.post("https://httpbin.org/post", body, #{
    "headers": #{"content-type": "application/json"}
})
println(r2["status"])

-- server: simple echo
http.serve(8080, fn(req) {
    return #{
        "status": 200,
        "body": "path was: {req[""path"]}"
    }
})