Request & Response
Every route handler function in Mayari receives a Request object as its first parameter. Your handler processes this request and returns a Response using Mayari's built-in response utility.
The Request Object
Even if your endpoint doesn't need input data, Mayari always passes the Request object into your method function.
Parameter Naming
We use req by convention, but Luau doesn't care what you call it. Mayari always passes the Request object as argument #1:
-- Works completely fine!
function Home.Get(payload)
print(payload.path)
endRequest Properties
Every request contains these fields:
| Property | Type | Description |
|---|---|---|
req.method | string | The HTTP method (e.g., "GET", "POST") |
req.path | string | The exact request URL path (e.g., "/users/123") |
req.body | any | Parsed request body payload (JSON tables, text, or form data) |
req.headers | table | Map of HTTP request headers (keys are always lowercase) |
req.params | table | Parameters captured from dynamic route folders ([id] or [...slugs]) |
req.query | table | Query string key-value pairs (e.g., ?search=luau) |
HEADER TIP
Incoming header keys are normalized to lowercase. Always access them using lowercase keys like req.headers["authorization"] or req.headers["content-type"].
Type Annotations (IRequest<T>)
For simple routes, you can use the global IRequest type. When working with dynamic route parameters or query params, use IRequest<T> to get editor autocompletion:
local common = require("common")
local response = common.response
local User = {}
type ReqParam = {
params: { id: string },
query: { verbose: string }
}
function User.Get(req: IRequest<ReqParam>)
local userId = req.params.id -- Autocomplete works here!
local isVerbose = req.query.verbose
return response.json({ userId = userId, verbose = isVerbose })
end
return UserNOTE ON TYPE PARSING
Types are for editor autocomplete and Luau type-checking. Mayari does not coerce types at runtime (e.g., dynamic URL parameters remain strings like "123", so convert them manually with tonumber() if needed).
The Response Utility (common.response)
Instead of constructing raw response payloads manually, use common.response. It automatically sets the correct Content-Type headers for you.
Helper Methods
response.json(table, config?)— Encodes a table as JSON (application/json).response.send(string, config?)— Sends plain text (text/plain).response.html(string, config?)— Sends HTML content (text/html).response.css(string, config?)— Sends CSS content (text/css).response.js(string, config?)— Sends JavaScript content (application/javascript).
Response Configuration Options
Every response method accepts an optional config table as its second parameter to set custom status codes or response headers:
local common = require("common")
local response = common.response
local User = {}
function User.Get(req: IRequest)
-- Custom 404 response
return response.json(
{ error = "User not found" },
{ status = 404 }
)
end
function User.Post(req: IRequest)
-- Custom status code and extra headers
return response.json(
{ message = "User created successfully" },
{
status = 201,
headers = {
["X-Powered-By"] = "Mayari"
}
}
)
end
return UserSummary Cheat Sheet
- Lowercase Headers: Always read headers using lowercase strings (
req.headers["x-api-key"]). - Type Safety: Annotate handlers with
IRequest<ReqType>for typedparamsandquerysupport. - Config Pattern: Pass status codes and headers in the second argument:
response.json(data, { status = 201, headers = { ... } }).