Skip to content
Draft

http

The http module provides HTTP client and server functionality.

import http
import { get, post, Server } from http
@import http#get
@response = @http#get("https://api.example.com/users")
@response#status @// 200
@response#body @// Response body as string
@import http#post
@body = '{"name": "Alice"}'
@response = @http#post("https://api.example.com/users", @body)
@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")
@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()
@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#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 String
@import http#Server
@server = @http#Server::new()
@server#get("/", fn(@req) =>
@http#Response::text("Hello, World!")
fin action.)
@server#listen(8080)
@server#get("/users/:id", fn(@req) =>
@id = @req#params["id"]
@http#Response::text("User: " + @id)
fin action.)
@server#get("/search", fn(@req) =>
@query = @req#query["q"]
@http#Response::text("Searching for: " + @query)
fin action.)
@server#get("/api/users", fn(@req) =>
@users = get_all_users()
@http#Response::json(@users)
fin action.)
@server#get("/", fn(@req) =>
@http#Response::html("<h1>Hello, World!</h1>")
fin action.)
@server#get("/static/*", fn(@req) =>
@path = @req#params["*"]
@http#Response#file("static/" + @path)
fin action.)
@server#get("/old", fn(@req) =>
@http#Response::redirect("/new")
fin action.)
@server#get("/not-found", fn(@req) =>
@http#Response::text("Not Found", status: 404)
fin action.)
@server#get("/api/data", fn(@req) =>
@response = @http#Response::json(@data)
@response#header("Cache-Control", "no-cache")
@response#header("X-Custom", "value")
return @response
fin action.)
@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 @response
fin action.)
@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 @response
fin action.)
@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.)
@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.)
@server#serve_static("./public")
@server#tls("cert.pem", "key.pem")
@server#listen(443)
@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 value
@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)
@import http#multipart
@form = @http#multipart#Form::new()
@form#field("name", "Alice")
@form#file("avatar", "photo.jpg")
@response = @client#post("/upload", @form)
@server#get("/events", fn(@req) =>
@sse = @http#SSE::new()
for @event in @data_stream
@sse#send(@event)
fin for.
return @sse
fin action.)
@server#listen(8080)
@process#on_signal("SIGTERM", fn() =>
@server#shutdown()
fin action.)