Skip to content

http

The http module provides an HTTP client for making requests and a server for handling them. Requires import http. Client functions are effect fn.

FunctionSignatureDescription
serve(Int, Fn[Request] -> Response) -> UnitStart HTTP server on port (effect)
FunctionSignatureDescription
response(Int, String) -> ResponsePlain text response with status
json(Int, String) -> ResponseJSON response with status
with_headers(Int, String, Map[String, String]) -> ResponseResponse with custom headers
redirect(String) -> Response302 redirect
FunctionSignatureDescription
status(Response, Int) -> ResponseSet status code
body(Response) -> StringGet body string
set_header(Response, String, String) -> ResponseSet header
get_header(Response, String) -> Option[String]Get header value
FunctionSignatureDescription
req_method(Request) -> StringHTTP method (GET, POST, etc.)
req_path(Request) -> StringURL path
req_body(Request) -> StringRequest body
req_header(Request, String) -> Option[String]Get request header
query_params(Request) -> Map[String, String]Query parameters as map
FunctionSignatureDescription
get(String) -> Result[String, String]Send GET request
post(String, String) -> Result[String, String]Send POST with body
put(String, String) -> Result[String, String]Send PUT with body
patch(String, String) -> Result[String, String]Send PATCH with body
delete(String) -> Result[String, String]Send DELETE request
request(String, String, String, Map[String, String]) -> Result[String, String]Custom request (method, url, body, headers)
import http
import json
// HTTP server
effect fn main(args: List[String]) -> Result[Unit, String] = {
http.serve(3000, (req) => {
let method = http.req_method(req)
let path = http.req_path(req)
match path {
"/" => http.response(200, "Hello!"),
"/api/data" => http.json(200, json.stringify(data)),
_ => http.response(404, "Not found"),
}
})
}
// HTTP client
effect fn fetch_data() -> Result[String, String] = {
let body = http.get("https://api.example.com/data")
ok(body)
}