HTTP routing reference
Define Vectrune REST routes, read path parameters, parse JSON request bodies, and return explicit HTTP responses.
App and route declarations
Declare @App with type = REST. A route section combines a method and URL, for example @Route/GET /health. Its run series executes statements in order.
The current REST router registers GET, POST, PUT, and DELETE. CRUD expands into those methods; PATCH and OPTIONS handlers are not registered by this router.
#!RUNE
@App
name = Greeting API
type = REST
port = 3000
swagger = true
@Schema/Greeting
name = string
enthusiastic = bool
@Route/GET /health
description = Check that the API is running
run:
respond 200 "OK"
@Route/GET /greetings/{name}
description = Read a name from the URL
run:
result = { name: name }
respond 200 result
@Route/POST /greetings
description = Validate and echo a greeting
expect = Greeting
run:
parse-json
validate body #Greeting
respond 201 body
URL parameters
Use braces in the route URL: /greetings/{name}. Parameters enter the execution context as strings, accessible directly as name or through path.params.name.
For numeric comparisons, convert a parameter deliberately with to-number. Do not assume an ID from the URL is already a JSON number.
curl -i http://127.0.0.1:3000/greetings/AdaFind records by ID
Native find and find-index compare two string IDs exactly, so 1.1 and 1.10 select different records. A numeric ID can still match a URL parameter with the same numeric value.
#!RUNE
@App
name = Find exact IDs
type = REST
@Route/GET/items/{id}
run:
items = [{ id: "1.1" }, { id: "1.10" }, { id: 5 }]
item = items.find it.id == id
index = items.find-index it.id == id
respond 200 { item: item, index: index }
Request bodies
POST and PUT handlers receive the raw request body as body. Run parse-json to replace body with its parsed JSON value, or assign parsed = parse-json body to preserve the raw body.
Each request gets a fresh execution context. Query parameters and general HTTP headers are not automatically exposed by this REST handler. Shared state requires an explicit memory or data-source operation.
Check whether a request field exists
After parse-json, use is-set body.response to check whether the request included response. Negative numbers and explicit null values count as present. A missing field returns false.
#!RUNE
@App
name = Response presence
type = REST
@Route/POST/response
run:
body = parse-json body
present = is-set body.response
respond 200 { present: present }
Responses and execution
respond 201 body returns status 201 and serializes the context variable body. respond 404 "Not found" returns a literal message. respond 202 { queued: true, queueId: id } evaluates a Rune object expression and serializes it as JSON. A response stops the current request step sequence.
Without an explicit response, the executor can return the last computed result with HTTP 200, falling back to OK when no result is available. A builtin execution error becomes HTTP 500. Use explicit responses and validation for predictable API contracts.
The native REST handler returns a status and a string body. Object serialization does not automatically select application/json; verify the wire response before building clients that depend on response headers.
Response message text expands {path} using request context, and {pretty path} inserts indented JSON. Unknown placeholders remain unchanged; escape literal braces with \{ and \}. For example, respond 200 "Echoed: {pretty data}" returns text rather than a JSON document. Direct variable responses retain their existing serialization.
Append ?pretty=1 to indent valid JSON from REST routes, CRUD handlers, /openapi.json, or @NotFound responses with text/plain, application/json or +json content types. The parameter is enabled by its presence, including ?pretty=0 or an empty value. Non-JSON bodies, static files, frontends, status codes, and response headers are unchanged.
curl 'http://127.0.0.1:3000/greetings/Ada?pretty=1'Unmatched routes
REST apps return a Vectrune HTML 404 page when Accept explicitly permits text/html. The page lists declared route methods and paths, including authenticated routes. Safe concrete GET paths are links; parameterized and non-GET routes are text. Set not_found_page = false under @App to avoid exposing the list. Requests without explicit HTML acceptance retain the empty 404.
An @NotFound run block overrides the default for unmatched requests regardless of Accept, even when the page is disabled. Set content_type for HTML or another media type, and use respond to choose the status and body. Matched routes, method mismatches, /assets and non-root static mounts retain their own responses; root static files are served first.
#!RUNE
@App
name = Example API
type = REST
port = 3000
@Route/GET/health
run:
respond 200 "OK"
# Remove this section to use the built-in HTML page for browser requests.
@NotFound
content_type = text/html; charset=utf-8
run:
respond 404 "<h1>Sorry, this page does not exist.</h1>"
Compare computed values
In a native if condition, each side of a comparison can be a numeric expression. For example, compare correct * 100 with pass_percent * total to check a threshold without rounding.
#!RUNE
@App
name = Arithmetic comparisons
type = REST
@Route/GET/score/below-threshold
run:
a = 0
b = 55
if a * 100 >= b * 4:
respond 200 { passed: true }
respond 200 { passed: false }
@Route/GET/score/above-threshold
run:
a = 1
b = 55
if a * 100 >= b:
respond 200 { passed: true }
respond 200 { passed: false }
Branching with match and switch
Use match <expression>: or switch <expression>: to branch on an evaluated variable or string expression across multiple cases without evaluating every branch.
Cases match literal values, identifiers, comma-separated lists ("guest", "anonymous":), or default fallbacks (else, default, _, *). Only the first matching branch executes its nested run steps.