HTTP API Reference

HTTP client and server implementation built on TCP networking

Overview

The HTTP module provides a full-featured HTTP/1.1 implementation with:

  • HTTP client for making requests (GET, POST, PUT, DELETE, etc.)
  • HTTP server with routing support
  • Request and response objects
  • Header management
  • Multiple HTTP methods and status codes

Constants

HTTP Methods

ConstantValueDescription
HTTP_GET1GET method
HTTP_POST2POST method
HTTP_PUT3PUT method
HTTP_DELETE4DELETE method
HTTP_PATCH5PATCH method
HTTP_HEAD6HEAD method
HTTP_OPTIONS7OPTIONS method

HTTP Status Codes

ConstantValueDescription
HTTP_OK200Success
HTTP_CREATED201Resource created
HTTP_ACCEPTED202Request accepted for processing
HTTP_NO_CONTENT204No content
HTTP_MOVED_PERMANENTLY301Resource moved permanently
HTTP_FOUND302Resource found at different URI
HTTP_NOT_MODIFIED304Resource not modified
HTTP_BAD_REQUEST400Bad request
HTTP_UNAUTHORIZED401Unauthorized
HTTP_FORBIDDEN403Forbidden
HTTP_NOT_FOUND404Not found
HTTP_METHOD_NOT_ALLOWED405HTTP method not allowed
HTTP_CONFLICT409Request conflict
HTTP_INTERNAL_ERROR500Internal server error
HTTP_NOT_IMPLEMENTED501Not implemented
HTTP_BAD_GATEWAY502Bad gateway
HTTP_SERVICE_UNAVAILABLE503Service unavailable

Buffer Constants

ConstantValueDescription
HTTP_MAX_HEADER_SIZE8192Maximum size for HTTP headers (8KB)
HTTP_MAX_BODY_SIZE1048576Maximum size for HTTP body (1MB)
HTTP_DEFAULT_BUFFER4096Default buffer size for reading (4KB)

Request

Request Struct

S Request {
    method: i64,
    path: str,
    version: str,
    headers: Headers,
    body: i64,
    body_len: i64
}

Request Methods

new

F new(method: i64, path: str) -> Request

Create a new HTTP request.

get / post / put / delete

F get(path: str) -> Request
F post(path: str) -> Request
F put(path: str) -> Request
F delete(path: str) -> Request

Create a request with the specified HTTP method.

with_body

F with_body(&self, data: i64, len: i64) -> Request

Set the request body.

with_json

F with_json(&self, json_str: str) -> Request

Set JSON body and Content-Type header.

F header(&self, name: str, value: str) -> Request

Add a header to the request.

Response

Response Struct

S Response {
    status: i64,
    status_text: str,
    version: str,
    headers: Headers,
    body: i64,
    body_len: i64
}

Response Methods

new / ok / not_found / bad_request / internal_error

F new(status: i64) -> Response
F ok() -> Response
F not_found() -> Response
F bad_request() -> Response
F internal_error() -> Response

Create response objects with common status codes.

with_body / with_text / with_json / with_html

F with_body(&self, data: i64, len: i64) -> Response
F with_text(&self, text: str) -> Response
F with_json(&self, json_str: str) -> Response
F with_html(&self, html: str) -> Response

Set response body with appropriate Content-Type.

Status checking

F is_success(&self) -> i64
F is_redirect(&self) -> i64
F is_client_error(&self) -> i64
F is_server_error(&self) -> i64

Check response status category.

Client

Client Struct

S Client {
    timeout_ms: i64,
    follow_redirects: i64,
    max_redirects: i64
}

Client Methods

new

F new() -> Client

Create a new HTTP client with default settings.

execute

F execute(&self, host: str, port: i64, request: &Request) -> Response?

Execute an HTTP request.

get / post

F get(&self, url: str) -> Response?
F post(&self, url: str, body: str) -> Response?

Convenience methods for common operations.

Server

Server Struct

S Server {
    host: str,
    port: i64,
    router: Router,
    running: i64
}

Server Methods

new

F new(port: i64) -> Server
F bind(host: str, port: i64) -> Server

Create a new HTTP server.

routes

F routes(&self, router: Router) -> Server

Add routes to the server.

run

F run(&self) -> i64

Run the server (blocking).

Handler Trait

Handler Trait

W Handler {
    F handle(&self, req: &Request) -> Response
}

The Handler trait defines the interface for request handlers. Any type implementing this trait can be used as a route handler in the HTTP server.

Route

Route Struct

S Route {
    method: i64,
    path: str,
    handler_ptr: i64
}

Represents a single route mapping an HTTP method and path to a handler function.

Router

Router Struct

S Router {
    routes: i64,
    count: i64,
    capacity: i64
}

Router Methods

new

F new() -> Router

Create a new router.

get / post / put / delete

F get(&self, path: str, handler: i64) -> Router
F post(&self, path: str, handler: i64) -> Router
F put(&self, path: str, handler: i64) -> Router
F delete(&self, path: str, handler: i64) -> Router

Add routes for specific HTTP methods.

Convenience Functions

F client() -> Client
F server(port: i64) -> Server
F router() -> Router
F get(url: str) -> Response?
F post(url: str, body: str) -> Response?

Quick access to common operations.

Usage Examples

HTTP Client - GET Request

# Simple GET request
response := get("http://api.example.com/users")

M response {
    Some(resp) => {
        I resp.is_success() == 1 {
            body := resp.body_text()
            # Process response body
        }
    }
    None => {
        # Handle error
    }
}

HTTP Client - POST Request

client := Client::new()
request := Request::post("/api/users")
    .with_json('{"name":"Alice","age":30}')
    .header("Authorization", "Bearer token123")

response := client.execute("api.example.com", 80, &request)

HTTP Server

# Define routes
router := router()
    .get("/", handle_index)
    .get("/users", handle_users)
    .post("/users", create_user)

# Start server
server := server(8080)
    .routes(router)
    .run()

Custom Handler

F handle_index(req: &Request) -> Response {
    Response::ok()
        .with_html("<h1>Welcome</h1>")
}

F handle_users(req: &Request) -> Response {
    json := '{"users":["Alice","Bob","Charlie"]}'
    Response::ok()
        .with_json(json)
}