Skip to content

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:

luau
-- Works completely fine!
function Home.Get(payload)
    print(payload.path)
end

Request Properties ​

Every request contains these fields:

PropertyTypeDescription
req.methodstringThe HTTP method (e.g., "GET", "POST")
req.pathstringThe exact request URL path (e.g., "/users/123")
req.bodyanyParsed request body payload (JSON tables, text, or form data)
req.headerstableMap of HTTP request headers (keys are always lowercase)
req.paramstableParameters captured from dynamic route folders ([id] or [...slugs])
req.querytableQuery 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:

lua
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 User

NOTE 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:

lua
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 User

Summary Cheat Sheet ​

  • Lowercase Headers: Always read headers using lowercase strings (req.headers["x-api-key"]).
  • Type Safety: Annotate handlers with IRequest<ReqType> for typed params and query support.
  • Config Pattern: Pass status codes and headers in the second argument: response.json(data, { status = 201, headers = { ... } }).