HTTP & Networking

Kodo provides built-in functions for making HTTP requests, running HTTP servers, and working with JSON. These builtins enable AI agents to build real web services and CLI tools without external dependencies.

HTTP Client

http_get(url) -> Result<String, String>

Performs an HTTP GET request. Returns Result::Ok(body) on success or Result::Err(message) on failure.

let result: Result<String, String> = http_get("http://httpbin.org/get")

http_post(url, body) -> Result<String, String>

Performs an HTTP POST request with a string body.

let result: Result<String, String> = http_post("http://httpbin.org/post", "hello")

Plain HTTP Only

In v1, HTTP builtins use plain HTTP (no TLS). If you need to reach an HTTPS endpoint, route through a local proxy or use a service that exposes an HTTP interface.

HTTP Server

Kodo includes a synchronous HTTP server powered by tiny_http. Servers and requests are managed via opaque Int handles.

http_server_new(port) -> Int

Creates a new HTTP server listening on the given port. Returns a handle, or 0 on failure.

let server: Int = http_server_new(8080)
if server == 0 {
    println("failed to create server")
}

http_server_recv(server) -> Int

Blocks until a request is received. Returns a request handle, or 0 on error.

let req: Int = http_server_recv(server)

http_request_method(req) -> String

Returns the HTTP method ("GET", "POST", etc.).

http_request_path(req) -> String

Returns the URL path (e.g., "/api/data").

http_request_body(req) -> String

Returns the request body as a string.

http_respond(req, status, body)

Sends an HTTP response with a status code and body. After calling this, the request handle is consumed and must not be reused.

http_respond(req, 200, "OK")

http_server_free(server)

Frees an HTTP server handle when done.

Complete Server Example

module http_api {
    meta {
        purpose: "HTTP server with JSON responses"
        version: "0.1.0"
    }

    fn main() -> Int {
        let server: Int = http_server_new(8080)
        if server == 0 {
            println("failed to create server")
            return 1
        }
        println("server listening on port 8080")

        let req: Int = http_server_recv(server)
        let method: String = http_request_method(req)
        let path: String = http_request_path(req)
        println(f"received {method} {path}")

        let body: Int = json_new_object()
        json_set_string(body, "status", "ok")
        let response: String = json_stringify(body)
        http_respond(req, 200, response)

        http_server_free(server)
        return 0
    }
}

JSON Parsing

Kodo represents parsed JSON as an opaque handle (an Int). You obtain a handle by parsing a JSON string, extract values from it by key, and free it when done.

json_parse(str) -> Int

Parses a JSON string and returns a handle. Returns 0 if parsing fails.

let handle: Int = json_parse("{\"count\": 42}")

json_get_int(handle, key) -> Int

Extracts an integer value by key. Returns 0 if the key does not exist.

json_get_string(handle, key) -> String

Extracts a string value by key.

json_free(handle)

Frees the memory associated with a parsed JSON handle. Passing 0 is safe.

JSON Builder

Build JSON objects programmatically without string manipulation.

json_new_object() -> Int

Creates a new empty JSON object and returns a handle.

let obj: Int = json_new_object()

json_set_string(handle, key, value)

Sets a string field on the JSON object.

json_set_int(handle, key, value)

Sets an integer field.

json_set_bool(handle, key, value)

Sets a boolean field.

json_stringify(handle) -> String

Serializes a JSON object to a string.

let obj: Int = json_new_object()
json_set_string(obj, "name", "kodo")
json_set_int(obj, "version", 1)
json_set_bool(obj, "stable", false)
let output: String = json_stringify(obj)
println(output)  // {"name":"kodo","stable":false,"version":1}

Summary of Builtins

HTTP Client

BuiltinParametersReturnsDescription
http_geturl: StringResult<String, String>GET request
http_posturl: String, body: StringResult<String, String>POST request

HTTP Server

BuiltinParametersReturnsDescription
http_server_newport: IntInt (handle)Create server
http_server_recvserver: IntInt (request handle)Block for request
http_request_methodreq: IntStringGet HTTP method
http_request_pathreq: IntStringGet URL path
http_request_bodyreq: IntStringGet request body
http_respondreq: Int, status: Int, body: String()Send response
http_server_freeserver: Int()Free server

JSON

BuiltinParametersReturnsDescription
json_parsestr: StringInt (handle)Parse JSON
json_get_inthandle: Int, key: StringIntExtract integer
json_get_stringhandle: Int, key: StringStringExtract string
json_freehandle: Int()Free JSON handle
json_new_objectInt (handle)Create empty object
json_set_stringhandle: Int, key: String, value: String()Set string field
json_set_inthandle: Int, key: String, value: Int()Set integer field
json_set_boolhandle: Int, key: String, value: Bool()Set boolean field
json_stringifyhandle: IntStringSerialize to string

Next Steps