Skip to Content
ControllerHeaders

Headers

Request headers

Request headers can be received using built-in Next.js headers function.

import { headers } from 'next/headers'; const reqHeaders = headers(); const userAgent = reqHeaders.get('user-agent');

Or using req.headers provided by Web Request API .

import { prefix, get } from 'vovk'; import { withZod } from 'vovk-zod'; @prefix('users') export default class UserController { @get('info') static async getUserInfo = withZod({ async handle(req) { const headers = req.headers; const userAgent = headers.get('user-agent'); // ... }, }); }

Cookies

To work with cookies you can use cookies  function exported by Next.js headers module.

import { cookies } from 'next/headers'; export default class UserController { @put('do-something') static async doSomething() { const cookieStore = await cookies(); const theme = cookieStore.get('theme'); // ... } }

Response headers

All HTTP decorators support custom response headers passed as the second argument option.

// ... export default class UserController { @put('do-something', { headers: { 'x-hello': 'world' } }) static async doSomething(/* ... */) { /* ... */ } }

To enable buuilt-in CORS headers you can use cors: true option.

// ... export default class UserController { @put('do-something', { cors: true }) static async doSomething(/* ... */) { /* ... */ } }

For auto-generated endpoints cors and headers are defined as the only argument.

// ... export default class UserController { @put.auto({ cors: true, headers: { 'x-hello': 'world' } }) static async doSomething(/* ... */) { /* ... */ } }

Dynamic response headers

To set up dynamic response headers you can use NextResponse object that is provided by Next.js.

import { NextResponse } from 'next/server'; // ... export default class UserController { @put('do-something') static async doSomething() { return NextResponse.json({ hello: 'world' }, { 'x-hello': 'world' }); } }
Last updated on