Problems with the NextAPIRequest
Answered
LeonTM posted this in #help-forum
LeonTMOP
Hi, I am building an api route at the moment. For those I wrote a function which validates the Access, meaning it just checks for the authorization header to be the value I want. Lets say it is "hello". But even if I set the authorization header in postman or within the fetch method is always undefined even if it is defined as hello in the request.headers map. What am I doing wrong here?
Answered by gin
import { headers } from 'next/headers'
const headersList = headers()
const auth = headersList.get('authorization')8 Replies
The headers map
The console log for the authorization header, sorry I isnt null, just undefined
@LeonTM The function
import { headers } from 'next/headers'
const headersList = headers()
const auth = headersList.get('authorization')Answer
LeonTMOP
Thanks for the quick help. But why is it like this?
@LeonTM Thanks for the quick help. But why is it like this?
cause the headers object is raw and needs to be parsed
@LeonTM Hi, I am building an api route at the moment. For those I wrote a function which validates the Access, meaning it just checks for the authorization header to be the value I want. Lets say it is "hello". But even if I set the authorization header in postman or within the fetch method is always undefined even if it is defined as hello in the request.headers map. What am I doing wrong here?
App router or pages router?
In the app router it is not NextApiRequest, it is NextRequest (or just Request). NextApiRequest != NextRequest
In the app router it is not NextApiRequest, it is NextRequest (or just Request). NextApiRequest != NextRequest