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
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
how can i do the same with Next Route handler
so the usual structure is something like this
but instead of this i need to set response header and send a buffer
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 response2 Replies
@Western Meadowlark 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
you can set the header like this
export async function GET(request: Request) {
return new Response(Buffer.from(), {
headers: {
"Content-Type": "applicatoin/pdf"
}
})
}Western MeadowlarkOP
so this worked for me
const response = new NextResponse(Buffer.from(buffer))
response.headers.set('Content-Type', 'application/pdf')
return responseAnswer