Request API
Complete API reference for Verb's enhanced request object with additional properties and methods for easier HTTP handling.
Interface
interface VerbRequest extends globalThis.Request {
params?: Record<string, string>; // Route parameters
query?: Record<string, string>; // Query string parameters
body?: any; // Parsed request body
cookies?: Record<string, string>; // Parsed cookies
ip?: string; // Client IP address
path?: string; // URL path
hostname?: string; // Request hostname
protocol?: string; // http/https
secure?: boolean; // HTTPS check
xhr?: boolean; // XMLHttpRequest check
get?: (header: string) => string | undefined;
accepts?: (types?: string | string[]) => string | string[] | null;
acceptsCharsets?: (charsets?: string | string[]) => string | string[] | null;
acceptsEncodings?: (encodings?: string | string[]) => string | string[] | null;
acceptsLanguages?: (languages?: string | string[]) => string | string[] | null;
}
Properties
params
Route parameters from URL patterns.
req.params: Record<string, string> | undefined
Example:
// Route: /users/:id/posts/:postId
app.get("/users/:id/posts/:postId", (req, res) => {
const { id, postId } = req.params;
// id and postId are strings
});
query
Query string parameters parsed from URL.
req.query: Record<string, string> | undefined
Example:
// URL: /search?q=typescript&page=2&limit=10
app.get("/search", (req, res) => {
const { q, page, limit } = req.query;
// All values are strings: "typescript", "2", "10"
});
body
Parsed request body (requires middleware).
req.body: any
Example:
app.use(middleware.json());
app.post("/users", (req, res) => {
const { name, email } = req.body;
// body is parsed JSON object
});
cookies
Parsed cookies from Cookie header.
req.cookies: Record<string, string> | undefined
Example:
app.get("/profile", (req, res) => {
const { sessionId, preferences } = req.cookies;
});
ip
Client IP address.
req.ip: string | undefined
path
URL pathname without query string.
req.path: string | undefined
hostname
Request hostname.
req.hostname: string | undefined
protocol
Request protocol (http or https).
req.protocol: string | undefined
secure
Whether the request is HTTPS.
req.secure: boolean | undefined
xhr
Whether the request is an XMLHttpRequest.
req.xhr: boolean | undefined
Methods
get(header)
Get request header value.
req.get(header: string): string | undefined
Example:
app.get("/api/data", (req, res) => {
const userAgent = req.get("user-agent");
const authorization = req.get("authorization");
const contentType = req.get("content-type");
});
accepts(types?)
Check what content types the client accepts.
req.accepts(types?: string | string[]): string | string[] | null
Example:
app.get("/data", (req, res) => {
const accepts = req.accepts(["json", "html", "xml"]);
switch (accepts) {
case "json":
res.json({ data: "json" });
break;
case "html":
res.html("<h1>HTML</h1>");
break;
case "xml":
res.type("application/xml").send("<data>xml</data>");
break;
default:
res.status(406).json({ error: "Not Acceptable" });
}
});
acceptsCharsets(charsets?)
Check what character sets the client accepts.
req.acceptsCharsets(charsets?: string | string[]): string | string[] | null
acceptsEncodings(encodings?)
Check what encodings the client accepts.
req.acceptsEncodings(encodings?: string | string[]): string | string[] | null
acceptsLanguages(languages?)
Check what languages the client accepts.
req.acceptsLanguages(languages?: string | string[]): string | string[] | null
Standard Request Properties
Verb requests also inherit all standard Request properties:
method
HTTP method (GET, POST, etc.).
req.method: string
url
Full request URL.
req.url: string
headers
Request headers.
req.headers: Headers
Example:
app.post("/upload", (req, res) => {
const contentType = req.headers.get("content-type");
const contentLength = req.headers.get("content-length");
// Iterate over all headers
for (const [name, value] of req.headers) {
console.log(`${name}: ${value}`);
}
});
signal
AbortSignal for request cancellation.
req.signal: AbortSignal
Request Body Methods
json()
Parse request body as JSON.
req.json(): Promise<any>
text()
Parse request body as text.
req.text(): Promise<string>
formData()
Parse request body as FormData.
req.formData(): Promise<FormData>
arrayBuffer()
Parse request body as ArrayBuffer.
req.arrayBuffer(): Promise<ArrayBuffer>
blob()
Parse request body as Blob.
req.blob(): Promise<Blob>
Examples
Basic Request Handling
app.get("/info", (req, res) => {
res.json({
method: req.method,
url: req.url,
path: req.path,
hostname: req.hostname,
protocol: req.protocol,
secure: req.secure,
ip: req.ip,
userAgent: req.get("user-agent")
});
});
Route Parameters
app.get("/users/:userId/posts/:postId", (req, res) => {
const { userId, postId } = req.params;
res.json({
user: userId,
post: postId,
url: `/users/${userId}/posts/${postId}`
});
});
Query Parameters
app.get("/search", (req, res) => {
const {
q = "",
page = "1",
limit = "10",
sort = "created"
} = req.query;
res.json({
query: q,
pagination: {
page: parseInt(page),
limit: parseInt(limit)
},
sort
});
});
Content Negotiation
app.get("/api/users", (req, res) => {
const users = getUsersFromDatabase();
const accepts = req.accepts(["json", "xml", "csv"]);
switch (accepts) {
case "json":
res.json(users);
break;
case "xml":
res.type("application/xml").send(usersToXML(users));
break;
case "csv":
res.type("text/csv").send(usersToCSV(users));
break;
default:
res.status(406).json({ error: "Not Acceptable" });
}
});
Request Validation
app.post("/api/users", (req, res) => {
// Validate content type
const contentType = req.get("content-type");
if (!contentType?.includes("application/json")) {
return res.status(400).json({
error: "Content-Type must be application/json"
});
}
// Validate required fields
const { name, email } = req.body;
if (!name || !email) {
return res.status(400).json({
error: "Name and email are required"
});
}
// Process valid request
const user = createUser({ name, email });
res.status(201).json(user);
});
TypeScript Types
interface VerbRequest extends globalThis.Request {
params?: Record<string, string>;
query?: Record<string, string>;
body?: any;
cookies?: Record<string, string>;
ip?: string;
path?: string;
hostname?: string;
protocol?: string;
secure?: boolean;
xhr?: boolean;
get(header: string): string | undefined;
accepts(types?: string | string[]): string | string[] | null;
acceptsCharsets(charsets?: string | string[]): string | string[] | null;
acceptsEncodings(encodings?: string | string[]): string | string[] | null;
acceptsLanguages(languages?: string | string[]): string | string[] | null;
}
type RouteHandler = (
req: VerbRequest,
res: VerbResponse,
next?: NextFunction
) => void | Promise<void>;
See Also
- Response API - Response object reference
- Routing API - Route handling
- Middleware API - Request processing middleware