Next.js Discord

Discord Forum

Api route call unable to get headers?

Answered
Bluetick Coonhound posted this in #help-forum
Open in Discord
Avatar
Bluetick CoonhoundOP
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 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
View full answer

83 Replies

Avatar
Bluetick CoonhoundOP
Here is the request i'm sending:
Image
the Api:
Image
and the Results:
Image
Avatar
what does the host var log (is it the null?)
Avatar
Bluetick CoonhoundOP
Yep, also null.
I've tried most things and even created a new project but that didn't work either
Avatar
its odd as i can do it here, can you try to not wrap it in header again...
Image
Avatar
Bluetick CoonhoundOP
oh yeah, kindly ignore that one, i was trying some work arounds using cookies-next but that didn't work as well
let me try
Avatar
and if that doesnt work, try from "next/headers"
Image
Avatar
Bluetick CoonhoundOP
oh, i've tried that as well, also null.
Avatar
...
do you have some weird middleware or somehow dropping those headers 😭
Avatar
Bluetick CoonhoundOP
yeah nope-
Image
Image
Avatar
Bluetick CoonhoundOP
I'm thinking real hard about it 😂
even went as far as to downloading Brave Browser just so i can have a clean start
will check
Avatar
can you make a vscode or cloudflare tunnel for it and for me to try 👀 (but i doubt i can see much issue)
Avatar
Bluetick CoonhoundOP
ohhh, that one's new, Should i go find out how to set that up first? :thinq:
Avatar
maybe try jasons thing as its an intresting thing
Avatar
Bluetick CoonhoundOP
yep, checking it out rn
Avatar
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
Image
Avatar
Bluetick CoonhoundOP
this thingy?
Image
Avatar
yeah
you put port 3000 and make it public
Avatar
Bluetick CoonhoundOP
ahh, let me try it
is this it?
i've just made it public
Avatar
i see it
and it says it send that header 😭
Avatar
Bluetick CoonhoundOP
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
Image
Avatar
btw you should JSON.stringify the body so it isnt just [object object]
Avatar
Bluetick CoonhoundOP
ahh, alright, gonna do just that
Avatar
i seeee it
Image
Avatar
Bluetick CoonhoundOP
Exaaactly but look at this when i use req.headers
Image
Avatar
the credentials thing didnt work?
Avatar
lol what depending on how you log it, it is shown
Avatar
Bluetick CoonhoundOP
nope, tried all 3
Avatar
can we temp make it a random header name like x-text just to make sure its not the name
Avatar
Bluetick CoonhoundOP
also tried making a Header(object?)
let me just try that
should i try accessing it via req.headers.get?
Avatar
yeah
Avatar
Bluetick CoonhoundOP
that didn't end well
Image
Avatar
well that at least worked for me on "latest" version
Avatar
Bluetick CoonhoundOP
Image
I just installed the latest version of Next-
Avatar
yeah
Avatar
Bluetick CoonhoundOP
unless these ain't it?
Image
Avatar
actually i am like 1 version behind now (but i doubt it would make dif)
Avatar
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
Answer
Avatar
👀
Avatar
Bluetick CoonhoundOP
yeah, about that
Avatar
yeah that would do it
Avatar
Bluetick CoonhoundOP
removing it gives me yet another error
Avatar
make it force-dynamic
Avatar
Bluetick CoonhoundOP
oh, that's a thing? :lolsob:
Avatar
well i usally just do export const revalidate = 0 lol
yeah
Image
Avatar
Bluetick CoonhoundOP
Image
HELL YEAH, WORKS
Avatar
YAYAYAYA
Avatar
Bluetick CoonhoundOP
:blob_wave:
THANKS LADS
Avatar
technically when using force-static, headers and cookies are gonna be null
Avatar
yeah nextjs would have precomputed it and kinda makes sense now...
Avatar
Bluetick CoonhoundOP
first time hearing it, i haven't tried out this Api Routing thing just yet after like...
2 years of using Next? 💀
Avatar
also this is why i just use sevrer actions now
Avatar
Bluetick CoonhoundOP
I'm catching up in Web
Server Actions?
Avatar
can you edit your message to include to use force-dynamic so i can give the point to you (as you found the issue)
Avatar
Bluetick CoonhoundOP
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!
Avatar
no need to close, marking it as answered is enough (most people dont, so i usually do it for them)
Avatar
Bluetick CoonhoundOP
Oh, well that's good to know