Request & response

What you read from the request, how bodies are parsed, and how responses carry types.

Request

PropertyNotes
urlNormalized pathname
methodHTTP method enum
headersNative Headers
bodyParsed body (schema type, else unknown)
queryQuery params; schema type or Record<string, string | undefined>
paramsRoute params; inferred from the URL or schema
cookiesParsed Cookie header, URI-decoded
ipClient IP, or null
dataContext filled by middlewares
rawOriginal Request

query, params, and cookies are read-only. You write to data and body.

Body parsing

Parsed from Content-Type. GET never gets a body. Malformed bodies yield null rather than throwing.

Content-Typereq.body
application/jsonParsed JSON
multipart/form-dataObject of fields; files as Web File
application/x-www-form-urlencodedObject of string fields
anything elseRaw text

For uploads, validation with z.file(), and sendFile, see Files.

Cookies

If the request has a Cookie header, each name=value pair is stored in req.cookies. Values are passed through decodeURIComponent; if decoding fails, the raw value is kept.

.get("/me", (req, res) => {
  const session = req.cookies.session;
  return res.json({ session: session ?? null });
})

Response

res.json({ message: "Success" });           // 200 JSON (typed for the client)
res.status(201).json({ created: true });  // custom status
res.send("Plain text");                   // raw body
res.redirect("/new-location", 301);       // redirect (default 307)
await res.sendFile("/path/to/file.pdf");  // stream a file
res.type("xml").send("<root></root>");    // Content-Type from extension
res.setHeader("X-Custom", "value").json({});
  • res.json() returns TypedResponse<T> - that phantom type is what HttpClient reads
  • send / redirect / sendFile return a plain Response and contribute no type information
  • type(ext) maps an extension (e.g. "json", "pdf") to a MIME type via @std/media-types
  • size(n) sets Content-Length