Api route call unable to get headers?
Answered
French Bulldog posted this in #help-forum
French BulldogOP
I've tried this in a new project and it seems to be still giving me a null despite how many solutions i've tried. I've checked: Github, Stackoverflow, NextJS Documentions and have found nothing that made it work.
Answered by jason
wait your
You will need to change it to
export const dynamic said force-staticYou will need to change it to
force-dynamic for headers or cookies to not show up as null83 Replies
French BulldogOP
Here is the request i'm sending:
the Api:
and the Results:
what does the
host var log (is it the null?)French BulldogOP
Yep, also null.
I've tried most things and even created a new project but that didn't work either
also fyi there is no res like that in route handlers: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#headers
its odd as i can do it here, can you try to not wrap it in header again...
French BulldogOP
oh yeah, kindly ignore that one, i was trying some work arounds using cookies-next but that didn't work as well
@riský its odd as i can do it here, can you try to not wrap it in header again...
French BulldogOP
let me try
French BulldogOP
oh, i've tried that as well, also null.
...
do you have some weird middleware or somehow dropping those headers ðŸ˜
French BulldogOP
yeah nope-
@riský do you have some weird middleware or somehow dropping those headers ðŸ˜
French BulldogOP
I'm thinking real hard about it 😂
even went as far as to downloading Brave Browser just so i can have a clean start
can you make a vscode or cloudflare tunnel for it and for me to try 👀 (but i doubt i can see much issue)
French BulldogOP
ohhh, that one's new, Should i go find out how to set that up first? 

maybe try jasons thing as its an intresting thing
French BulldogOP
yep, checking it out rn
vscode one is the simplist, but i use cf tunnel usally as i have it set up with custom domain so its always the same url
French BulldogOP
this thingy?
yeah
you put port 3000 and make it public
French BulldogOP
ahh, let me try it
is this it?
i've just made it public
i see it
and it says it send that header ðŸ˜
French BulldogOP
exactly, it's sending a header
yet i can't access it
what's even weirder is this, let me just send it
there's a list of headers as you can see
btw you should JSON.stringify the body so it isnt just [object object]
French BulldogOP
ahh, alright, gonna do just that
@riský *i seeee it*
French BulldogOP
Exaaactly but look at this when i use req.headers
the credentials thing didnt work?
lol what depending on how you log it, it is shown
@jason the credentials thing didnt work?
French BulldogOP
nope, tried all 3
can we temp make it a random header name like
x-text just to make sure its not the nameFrench BulldogOP
also tried making a Header(object?)
@riský can we temp make it a random header name like `x-text` just to make sure its not the name
French BulldogOP
let me just try that
should i try accessing it via req.headers.get?
yeah
French BulldogOP
that didn't end well
well that at least worked for me on "latest" version
French BulldogOP
@riský well that at least worked for me on "latest" version
French BulldogOP
I just installed the latest version of Next-
yeah
French BulldogOP
unless these ain't it?
actually i am like 1 version behind now (but i doubt it would make dif)
wait your
You will need to change it to
export const dynamic said force-staticYou will need to change it to
force-dynamic for headers or cookies to not show up as nullAnswer
👀
yeah that would do it
French BulldogOP
removing it gives me yet another error
make it force-dynamic
French BulldogOP
oh, that's a thing? 

well i usally just do export const revalidate = 0 lol
French BulldogOP
HELL YEAH, WORKS
YAYAYAYA
French BulldogOP

THANKS LADS
technically when using
force-static, headers and cookies are gonna be nullyeah nextjs would have precomputed it and kinda makes sense now...
@jason technically when using `force-static`, headers and cookies are gonna be null
French BulldogOP
first time hearing it, i haven't tried out this Api Routing thing just yet after like...
2 years of using Next? 💀
also this is why i just use sevrer actions now
French BulldogOP
I'm catching up in Web
@riský also this is why i just use sevrer actions now
French BulldogOP
Server Actions?
@jason wait your `export const dynamic` said `force-static`
You will need to change it to `force-dynamic` for headers or cookies to not show up as `null`
can you edit your message to include to use
force-dynamic so i can give the point to you (as you found the issue)French BulldogOP
This took me a long time ever since last night, thanks again, you guys
i might drop by again since i'm slowly migrating to NextJS 14
I'll be closing this post then!
no need to close, marking it as answered is enough (most people dont, so i usually do it for them)
French BulldogOP
Oh, well that's good to know