Accessing response headers in API routes of App Router? #83695
Replies: 2 comments
-
|
In App Router, you control response headers directly by constructing and returning a Setting and reading response headers// app/api/example/route.ts
import { NextRequest, NextResponse } from 'next/server'
export async function GET(request: NextRequest) {
const response = NextResponse.json({ message: 'Hello' })
// Set any response headers you want
response.headers.set('X-Custom-Header', 'my-value')
response.headers.set('Access-Control-Allow-Origin', '*')
// Read headers you've set (or that NextResponse added)
console.log(response.headers.get('content-type')) // application/json
return response
}CORS headers specificallyFor CORS headers configured via If you need to inspect or conditionally set CORS headers inside the handler itself, set them explicitly instead of relying on // app/api/data/route.ts
export async function GET(request: NextRequest) {
const origin = request.headers.get('origin') || ''
const response = NextResponse.json({ data: 'value' })
// Set CORS headers explicitly
response.headers.set('Access-Control-Allow-Origin', origin)
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization')
return response
}
// Handle preflight
export async function OPTIONS(request: NextRequest) {
const origin = request.headers.get('origin') || ''
return new NextResponse(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Max-Age': '86400',
},
})
}Reading cookies from the responseFor export async function POST(request: NextRequest) {
const response = NextResponse.json({ success: true })
// Set cookies
response.cookies.set('session', 'abc123', {
httpOnly: true,
secure: true,
sameSite: 'lax',
maxAge: 60 * 60 * 24,
})
// Read what you've set
const sessionCookie = response.cookies.get('session')
console.log(sessionCookie) // { name: 'session', value: 'abc123', ... }
return response
}TL;DR: In App Router, you don't read response headers — you create them. The response object is yours to construct. For CORS or other headers added by |
Beta Was this translation helpful? Give feedback.
-
|
In Next.js App Router, you can access and set response headers in Route Handlers like this: Reading request headers: ts ts ts |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
API route handlers in Pages Router had access to both Request and Response objects, so you could read response headers like
Set-Cookie,Access-Control-Allow-Originand others.App Router's GET, POST and other handler functions only accept a Request object.
Now,
NextRequesttype allows to accessSet-Cookie. But what about other headers, like CORS set by next.config rules?Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions