Skip to content

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.

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>

Section titled “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")

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.

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

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")
}

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

let req: Int = http_server_recv(server)

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

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

Returns the request body as a string.

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")

Frees an HTTP server handle when done.

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
}
}

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.

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

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

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

Extracts a string value by key.

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

Build JSON objects programmatically without string manipulation.

Creates a new empty JSON object and returns a handle.

let obj: Int = json_new_object()

Sets a string field on the JSON object.

Sets an integer field.

Sets a boolean field.

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}
BuiltinParametersReturnsDescription
http_geturl: StringResult<String, String>GET request
http_posturl: String, body: StringResult<String, String>POST request
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
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