Next.js Discord

Discord Forum

Api route call unable to get headers?

Answered
Bluetick Coonhound posted this in #help-forum
Open in Discord
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

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