Next.js Discord

Discord Forum

Can't find headers/cookies in nextjs

Hilsa shad posted this in #help-forum
Open in Discord
Hilsa shadOP

This is my frist time using nextjs. In a personal project I'm running nextjs and have a separate backend api.

I want to query data that requires authentication in the backend api. I want the request to go client -> nextjs -> backend rather than making the request client side.

When I visit /myURL, I can see in my browser that the cookies are sent in the headers to /myURL, but I can't access the cookies or any headers with nextjs and therefore can't send them to the backend.

I'm trying to access them in the page.js in folder myURL.

import { cookies, headers } from 'next/headers' export default async function myURL() { // async function getServerSideProps(context) { // const myCookies = context.req.headers.cookie; // console.log(myCookies, 'potato') // return { // props: {}, // }; // } // getServerSideProps() console.log(headers(), 'headers') ....

Can anyone help me identify how I should properly access the headers/cookie?

1 Reply

Hilsa shadOP
I can access the cookies via cookies() in page.js of folder myURL, but can't access them via headers(). I'll read the docs on headers, but am unclear why when I call headers() I receive:

HeadersList { cookies: null, [Symbol(headers map)]: Map(0) {}, [Symbol(headers map sorted)]: null }