http
The http module provides HTTP client and server functionality.
Import
Section titled “Import”import httpimport { get, post, Server } from httpHTTP Client
Section titled “HTTP Client”GET Request
Section titled “GET Request”@import http#get
@response = @http#get("https://api.example.com/users")@response#status @// 200@response#body @// Response body as stringPOST Request
Section titled “POST Request”@import http#post
@body = '{"name": "Alice"}'@response = @http#post("https://api.example.com/users", @body)Other Methods
Section titled “Other Methods”@http#put("https://api.example.com/users/1", @body)@http#patch("https://api.example.com/users/1", @body)@http#delete("https://api.example.com/users/1")@http#head("https://api.example.com/users")@http#options("https://api.example.com/users")Request with Headers
Section titled “Request with Headers”@import http#Request
@request = @http#Request::new("GET", "https://api.example.com/users")@request#header("Authorization", "Bearer token123")@request#header("Accept", "application/json")@response = @request#send()Request with Options
Section titled “Request with Options”@import http#Client
@client = @http#Client::new()@client#timeout(seconds(30))@client#header("User-Agent", "MyApp/1.0")
@response = @client#get("https://api.example.com/users")@response = @client#post("https://api.example.com/users", @body)Response Object
Section titled “Response Object”@response#status @// Status code (Int)@response#status_text @// "OK", "Not Found", etc.@response#headers @// Map<String, String>@response#body @// Body as String@response#body_bytes @// Body as Array<Byte>@response#content_type @// Content-Type header@response#json() @// Parse body as JSON@response#text() @// Body as StringHTTP Server
Section titled “HTTP Server”Basic Server
Section titled “Basic Server”@import http#Server
@server = @http#Server::new()
@server#get("/", fn(@req) => @http#Response::text("Hello, World!")fin action.)
@server#listen(8080)Route Parameters
Section titled “Route Parameters”@server#get("/users/:id", fn(@req) => @id = @req#params["id"] @http#Response::text("User: " + @id)fin action.)Query Parameters
Section titled “Query Parameters”@server#get("/search", fn(@req) => @query = @req#query["q"] @http#Response::text("Searching for: " + @query)fin action.)JSON Response
Section titled “JSON Response”@server#get("/api/users", fn(@req) => @users = get_all_users() @http#Response::json(@users)fin action.)HTML Response
Section titled “HTML Response”@server#get("/", fn(@req) => @http#Response::html("<h1>Hello, World!</h1>")fin action.)File Response
Section titled “File Response”@server#get("/static/*", fn(@req) => @path = @req#params["*"] @http#Response#file("static/" + @path)fin action.)Redirect
Section titled “Redirect”@server#get("/old", fn(@req) => @http#Response::redirect("/new")fin action.)Custom Status
Section titled “Custom Status”@server#get("/not-found", fn(@req) => @http#Response::text("Not Found", status: 404)fin action.)Set Headers
Section titled “Set Headers”@server#get("/api/data", fn(@req) => @response = @http#Response::json(@data) @response#header("Cache-Control", "no-cache") @response#header("X-Custom", "value") return @responsefin action.)Middleware
Section titled “Middleware”@server#use(fn(@req, @next) => @start = @time#now() @response = @next() @duration = @time#elapsed(@start) @io#write_line(@req#method + " " + @req#path + " took " + string(@duration) + "ms") return @responsefin action.)CORS Middleware
Section titled “CORS Middleware”@server#use(fn(@req, @next) => @response = @next() @response#header("Access-Control-Allow-Origin", "*") @response#header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE") @response#header("Access-Control-Allow-Headers", "Content-Type, Authorization") return @responsefin action.)Authentication Middleware
Section titled “Authentication Middleware”@server#use(fn(@req, @next) => @token = @req#headers["Authorization"] if @token == nil or not @valid_tokens#contains(@token) return @http#Response::text("Unauthorized", status: 401) fin if. return @next()fin action.)WebSocket
Section titled “WebSocket”@server#websocket("/ws", fn(@socket) => @socket#on_message(fn(@message) => @socket#send("Echo: " + @message) fin action.) @socket#on_close(fn() => @io#write_line("Client disconnected") fin action.)fin action.)Static File Serving
Section titled “Static File Serving”@server#serve_static("./public")TLS/HTTPS
Section titled “TLS/HTTPS”@server#tls("cert.pem", "key.pem")@server#listen(443)Request Object
Section titled “Request Object”@req#method @// "GET", "POST", etc.@req#path @// Request path@req#query @// Query parameters map@req#params @// Route parameters map@req#headers @// Headers map@req#body @// Request body as string@req#json() @// Parse body as JSON@req#form() @// Parse form data@req#ip @// Client IP address@req#user_agent @// User-Agent header@req#cookie(@name) @// Get cookie valueClient Configuration
Section titled “Client Configuration”@import http#Client
@client = @http#Client::new() #timeout(seconds(30)) #header("User-Agent", "MyApp/1.0") #cookie("session", "abc123") #base_url("https://api.example.com") #auth("bearer", "token123")
@response = @client#get("/users")@response = @client#post("/users", @body)File Upload
Section titled “File Upload”@import http#multipart
@form = @http#multipart#Form::new()@form#field("name", "Alice")@form#file("avatar", "photo.jpg")
@response = @client#post("/upload", @form)Server-Sent Events
Section titled “Server-Sent Events”@server#get("/events", fn(@req) => @sse = @http#SSE::new() for @event in @data_stream @sse#send(@event) fin for. return @ssefin action.)Graceful Shutdown
Section titled “Graceful Shutdown”@server#listen(8080)
@process#on_signal("SIGTERM", fn() => @server#shutdown()fin action.)