Api route call unable to get headers?
Answered
Bluetick Coonhound posted this in #help-forum
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
You will need to change it to
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
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
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...
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
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-
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
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?
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 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
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 nameBluetick CoonhoundOP
also tried making a Header(object?)
let me just try that
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
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
You will need to change it to
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
👀
Bluetick CoonhoundOP
yeah, about that
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?
well i usally just do export const revalidate = 0 lol
yeah
Bluetick CoonhoundOP
HELL YEAH, WORKS
YAYAYAYA
Bluetick CoonhoundOP
THANKS LADS
technically when using
force-static
, headers and cookies are gonna be nullyeah nextjs would have precomputed it and kinda makes sense now...
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
Server Actions?
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