Next.js Discord

Discord Forum

how to set custom Response header in Next 13.5 GET route handler and send Buffer object as response

Answered
Western Meadowlark posted this in #help-forum
Open in Discord
Western MeadowlarkOP
I have written an express endpoint which I'm trying to convert to a Next 13 route handler
I have the below 2 lines at the end in my express get endpoint

res.setHeader('Content-Type', 'application/pdf');
    
res.status(200).send(Buffer.from(buffer));


how can i do the same with Next Route handler

so the usual structure is something like this

export async function GET(request: Request) {
   return Response.json({....})
}

but instead of this i need to set response header and send a buffer
Answered by Western Meadowlark
so this worked for me
const response = new NextResponse(Buffer.from(buffer))

response.headers.set('Content-Type', 'application/pdf')

return response
View full answer

2 Replies

Western MeadowlarkOP
so this worked for me
const response = new NextResponse(Buffer.from(buffer))

response.headers.set('Content-Type', 'application/pdf')

return response
Answer