http

HTTP server, routing, middleware, client, and CORS.

benutzen "http". Two server styles: **serve** (one handler for every request) or **listen** + **get/post/put/delete** (path routing).

Handlers receive req with method, path, body, headers, and (on matched routes) params. Return http::response, http::jsonResponse, or http::serveFile.

Pass handlers as named functions or **anonymous functions**: http::get("/", fun(req) { ... }) is idiomatic for short routes.

Wrap handler bodies in **versuch / fang** (try/catch) to catch runtime errors and return 500 without crashing the server. See **Versuch / fang (try/catch)** under Control flow.

Use **http::use** to register middleware. Return a response object to short-circuit; otherwise the chain continues to the route handler.

Client calls are **fetch** and **fetchPost**: not get/post, which register routes.

Requires libcurl at build time.

Anonymous route handlers

Pass a fun(req) directly to http::get or http::post.

server.rn
benutzen "http";

http::cors(true);

http::get("/", fun(req) {
  return http::response(200, "anonymous works!");
});

http::get("/test/:id", fun(req) {
  return http::response(200, "id: " + req.params.id);
});

http::listen(8080);

Router with middleware

Register routes on a router and stack middleware with http::use.

server.rn
benutzen "http";

fun logger(req) {
  print req.method + " " + req.path;
}

fun home(req) {
  return http::response(200, "hello");
}

fun createUser(req) {
  var name = http::header(req, "content-type");
  return http::jsonResponse(201, "{\"ok\":true}");
}

http::cors(true);
http::use(logger);
http::get("/", home);
http::post("/users", createUser);
http::listen(8080);

Route params

Read :id segments from req.params in path patterns.

routes.rn
benutzen "http";

fun showUser(req) {
  var id = req.params.id;
  return http::response(200, "user " + id);
}

http::get("/users/:id", showUser);
http::listen(8080);

Error handling with versuch / fang (try/catch)

Catch handler errors and return a 500 instead of crashing.

safe-handler.rn
benutzen "http";
benutzen "json";

fun predict(req) {
  versuch {
    var data = json::parse(req.body);
    if (data.pixels->len() != 784) {
      return http::response(400, "expected 784 pixels");
    }
    return http::jsonResponse(200, "{\"digit\":3}");
  } fang (err) {
    return http::response(500, err);
  }
}

http::post("/api/predict", predict);
http::listen(8080);

Single-handler server

One callback handles every request: good for tiny servers.

SymbolDescriptionExample
serve(port, handler)Blocking server: every request goes to one handler
benutzen "http"; fun h(req) { return http::response(200, "ok"); } http::serve(8080, h);

Router

Named routes for GET, POST, PUT, and DELETE.

SymbolDescriptionExample
listen(port)Start server after registering routes
benutzen "http"; http::get("/", home); http::listen(8080);
get(path, handler)Register GET route; `:name` captures params
benutzen "http"; http::get("/users/:id", showUser);
post(path, handler)Register POST route
benutzen "http"; http::post("/api/predict", predict);
put(path, handler)Register PUT route
benutzen "http"; http::put("/users/:id", updateUser);
delete(path, handler)Register DELETE route
benutzen "http"; http::delete("/users/:id", removeUser);

Middleware

Run shared logic before handlers: logging, auth, etc.

SymbolDescriptionExample
use(middleware)Run before each route handler; return a response to stop the chain
benutzen "http"; fun auth(req) { if (http::header(req, "authorization") == nil) { return http::response(401, "unauthorized"); } } http::use(auth);

Responses

Build status, body, and content-type for handlers.

SymbolDescriptionExample
response(status, body)Plain-text response (`type` defaults to `text/plain`)
benutzen "http"; return http::response(200, "hello");
jsonResponse(status, json)JSON response (`type` is `application/json`)
benutzen "http"; return http::jsonResponse(200, "{\"ok\":true}");
serveFile(status, path)Read a file; MIME type inferred from extension
benutzen "http"; return http::serveFile(200, "index.html");
cors(enabled, origin?)Add CORS headers; optional origin (default `*`). Handles OPTIONS preflight.
benutzen "http"; http::cors(true, "http://localhost:3000");

Client

fetch and fetchPost for outbound HTTP calls.

SymbolDescriptionExample
fetch(url)HTTP GET → `{ status, body }`
benutzen "http"; var r = http::fetch("https://example.com"); print r.status;
fetchPost(url, body)HTTP POST with JSON body → `{ status, body }`
benutzen "http"; var r = http::fetchPost("https://httpbin.org/post", "{}"); print r.status;

Request helpers

Read headers and other request metadata.

SymbolDescriptionExample
header(req, name)Read a request header (names are lowercase)
benutzen "http"; var ct = http::header(req, "content-type");

Request fields (in handler)

Fields available on the req object in a handler.

SymbolDescriptionExample
req.methodHTTP method string
fun h(req) { print req.method; }
req.pathURL path (query string stripped on routed requests)
fun h(req) { print req.path; }
req.bodyRequest body string
fun h(req) { print req.body; }
req.headersObject of lowercase header names → values
fun h(req) { print req.headers.host; }
req.paramsRoute capture object (`:id` → `req.params.id`)
fun h(req) { print req.params.id; }

Response object fields

Fields you set on the response returned from a handler.

SymbolDescriptionExample
statusHTTP status code (number)
var r = http::response(200, "ok"); print r.status;
bodyResponse body string
var r = http::response(200, "ok"); print r.body;
typeContent-Type header value
var r = http::jsonResponse(200, "{}"); print r.type;