HTTP & JSON
Returning JSON
get "/health" {
def health(req) {
return ok({ "status": "ok" })
}
}
post "/items" {
def create(req) {
let body = require_json(req)
require_fields(body, ["name"])
return created({ "id": 1, "ok": true })
}
}
json(body) and ok(body) are equivalent (HTTP 200). Prefer created(body)
for 201 and json(body, code) when you need another status.
Status-only
return no_content() // 204
// or: return status(204)
Path params
get "/users/{id}" {
def show(req) {
let id = req.param("id")
return ok({ "id": id })
}
}
Headers
let auth = req.header("authorization")
// or early-exit: let uid = require_auth(req)
Content types
| Helper | Typical Content-Type |
|---|---|
json(...) / ok(...) / created(...) | application/json |
html(...) / view(...) | text/html; charset=utf-8 |
Errors
Unhandled runtime errors become 500 JSON with a request_id. Prefer
explicit helpers (stable error codes):
return not_found()
return not_found("user missing")
return unauthorized()
return forbidden("no access")
return bad_request("bad id")
return unprocessable({ "email": "required" })
These desugar to http_json_response with { error, message? } or
{ error: "validation_failed", fields? }. Classic
json({ "error": "not_found" }, 404) still works.
CORS & body limits
| Env | Meaning |
|---|---|
ATL_CORS_ORIGINS | Comma-separated origins |
ATL_MAX_BODY_BYTES | Max body size (default ~10 MiB) |
Next: HTML views · Natives.