Skip to content

Request & response

Handlers receive a Request and return a Response. The framework calls response.deliver (with optional CORS) after routing.

Function Returns Description
query(req, key) String Query parameter value
param(req, key) String Path param from :key segment (empty if no pattern)
header(req, name) String Request header (case-insensitive lookup via runtime)
body(req, max_len) String Read body up to max_len bytes (empty on failure)
body_result(req, max_len) Result<String, int> Body read with error code
context_value(req) String Middleware-parsed body stored on request
with_context(req, ctx) Request Attach context (used by middleware)
parse_json(req, max_len) Result<String, int> Read + validate JSON body
import lib.request
import lib.response
fn show(req: request.Request) -> response.Response {
let q = request.query(req, "q");
let id = request.param(req, "id");
let auth = request.header(req, "Authorization");
return response.json("{\"q\":\"" + q + "\",\"id\":\"" + id + "\"}");
}

After use_json middleware, prefer context_value(req) for the parsed JSON string instead of re-reading the body.

Function Status Content-Type
send(body) 200 inferred (plain)
send(status, body) given inferred (plain)
send_typed(status, ct, body) given explicit
json(body) 200 application/json; charset=utf-8
json(status, body) given application/json; charset=utf-8
send_status(status) given empty body
redirect(url) 302 Location header
redirect(status, url) given Location header
Function Status
not_found(body) 404
bad_request(body) 400
payload_too_large(body) 413
unauthorized(body) 401
server_error(body) 500
fn create_item(req: request.Request) -> response.Response {
let body = request.body(req, 65536);
if len(body) == 0 {
return response.bad_request("{\"error\":\"empty body\"}");
}
return response.json(201, "{\"ok\":true}");
}
return response.redirect("/login"); // 302
return response.redirect(301, "/canonical"); // permanent

The runtime sets the Location header via http_reply_cors.

When using import vws from vow_web_server, request/response helpers are also on the barrel:

let id = vws.param(req, "id");
return vws.json(201, "{\"ok\":true}");
return vws.redirect("/home");

Submodule imports (vow_web_server.request, vow_web_server.response) remain the typed option for handler signatures.

use_cors(a, origin) wraps delivered responses with Access-Control-Allow-Origin. OPTIONS preflight returns 204 before middleware/route dispatch when CORS is enabled.