Async HTTP API Reference
Non-blocking HTTP/1.1 server and client built on async networking
Import
U std/async_http
Overview
The async_http module provides a complete async HTTP/1.1 framework with routing, middleware support, request/response parsing, connection pooling, and a client API. It is designed for building non-blocking web services.
Constants
HTTP Methods
| Constant | Value |
|---|---|
ASYNC_HTTP_GET | 1 |
ASYNC_HTTP_POST | 2 |
ASYNC_HTTP_PUT | 3 |
ASYNC_HTTP_DELETE | 4 |
ASYNC_HTTP_PATCH | 5 |
ASYNC_HTTP_HEAD | 6 |
ASYNC_HTTP_OPTIONS | 7 |
HTTP Status Codes
| Constant | Value |
|---|---|
STATUS_OK | 200 |
STATUS_CREATED | 201 |
STATUS_NO_CONTENT | 204 |
STATUS_NOT_FOUND | 404 |
STATUS_INTERNAL_ERROR | 500 |
Content Types
| Constant | Value | MIME |
|---|---|---|
CT_TEXT | 1 | text/plain |
CT_HTML | 2 | text/html |
CT_JSON | 3 | application/json |
CT_FORM | 4 | application/x-www-form-urlencoded |
CT_OCTET | 5 | application/octet-stream |
Structs
AsyncHttpRequest
Parsed HTTP request from a client.
Fields: method, path, query, version, headers, header_count, body, body_len, content_type, keep_alive
Methods:
new() -> AsyncHttpRequest-- Create an empty requestmethod_str(&self) -> str-- Get HTTP method as stringis_get(&self) -> i64-- Check if GET requestis_post(&self) -> i64-- Check if POST requesthas_body(&self) -> i64-- Check if request has a bodyget_header(&self, name: str) -> str-- Get header value by name
AsyncHttpResponse
HTTP response built by a handler.
Fields: status, headers, header_count, body, body_len, content_type
Methods:
new(status: i64) -> AsyncHttpResponse-- Create with status codeok() -> AsyncHttpResponse-- Create a 200 OK responsenot_found() -> AsyncHttpResponse-- Create a 404 responsebad_request() -> AsyncHttpResponse-- Create a 400 responseinternal_error() -> AsyncHttpResponse-- Create a 500 responsewith_text(&mut self, text: str) -> i64-- Set text bodywith_html(&mut self, html: str) -> i64-- Set HTML bodywith_json(&mut self, json: str) -> i64-- Set JSON bodyadd_header(&mut self, name: str, value: str) -> i64-- Add a response headerstatus_text(&self) -> str-- Get status text (e.g., "OK")content_type_str(&self) -> str-- Get MIME type string
Router
Collection of routes mapping method+path to handler functions.
Methods:
new() -> Router-- Create an empty routerget(&mut self, path: str, handler: i64) -> i64-- Add GET routepost(&mut self, path: str, handler: i64) -> i64-- Add POST routeput(&mut self, path: str, handler: i64) -> i64-- Add PUT routedelete(&mut self, path: str, handler: i64) -> i64-- Add DELETE routefind_handler(&self, method: i64, path: str) -> i64-- Find handler for request
AsyncHttpServer
HTTP server with routing and middleware.
Methods:
new(host: str, port: i64) -> AsyncHttpServer-- Create a serverroute_get/post/put/delete(&mut self, path: str, handler: i64)-- Add routesuse_middleware(&mut self, mw: Middleware) -> i64-- Add middlewarebind(&mut self) -> i64-- Start listeningpoll_once(&mut self) -> i64-- Process one requestrun_iterations(&mut self, max_iter: i64) -> i64-- Run accept loopstop(&mut self) -> i64-- Stop the serverstats_requests/stats_connections(&self) -> i64-- Get statistics
AsyncHttpClient
HTTP client for making requests.
Methods:
new() -> AsyncHttpClient-- Create a clientwith_base_url(url: str) -> AsyncHttpClient-- Create with base URLget(&self, path: str) -> AsyncHttpResponse-- GET requestpost(&self, path: str, body: str) -> AsyncHttpResponse-- POST requestput(&self, path: str, body: str) -> AsyncHttpResponse-- PUT requestdelete(&self, path: str) -> AsyncHttpResponse-- DELETE requestpost_json(&self, path: str, json: str) -> AsyncHttpResponse-- POST JSONset_timeout(&mut self, ms: i64) -> i64-- Set timeoutadd_default_header(&mut self, name: str, value: str) -> i64-- Add default header
ConnectionPool
Reusable connection pool for managing file descriptors.
Methods:
new(capacity: i64) -> ConnectionPoolacquire(&mut self) -> i64-- Get a connection fdrelease(&mut self, fd: i64) -> i64-- Return a connectionavailable(&self) -> i64-- Count of available connections
Convenience Functions
F async_http_server(host: str, port: i64) -> AsyncHttpServer
F async_http_client() -> AsyncHttpClient
F async_http_client_for(url: str) -> AsyncHttpClient
F text_response(status: i64, text: str) -> AsyncHttpResponse
F json_response(status: i64, json: str) -> AsyncHttpResponse
F html_response(status: i64, html: str) -> AsyncHttpResponse
F connection_pool(capacity: i64) -> ConnectionPool
F logging_middleware() -> Middleware
F cors_middleware() -> Middleware
Example
U std/async_http
F main() {
server := mut async_http_server("127.0.0.1", 8080)
server.route_get("/health", health_handler)
server.bind()
server.run_iterations(100)
server.stop()
}