cookies on behaving as expected on API routes
See original GitHub issueVerify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro
Binaries:
Node: 18.12.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.0.1-canary.4
eslint-config-next: 13.0.0
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
You're importing a component that needs next/headers. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
,----
3 | import { cookies } from 'next/headers';
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----
One of these is marked as a client entry with "use client":
pages\api\hello.tsx
Expected Behavior
probably return cookes as expected.
Link to reproduction
no link
To Reproduce
//pages/api/hello.tsx
import { cookies } from 'next/headers';
export default async function handler(req: any, res: any) {
console.log(cookies())
}
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:6
Top Results From Across the Web
Can't set Cookies in browser even though header is present
I made a new api route that just creates a cookie and sends an object to the client. server/routes/todo.routes.js router.get('/todos', (req, ...
Read more >Working with routes for WebSocket APIs - Amazon API Gateway
A route includes a route key, which is the value that is expected once a route selection expression is evaluated. The routeSelectionExpression is...
Read more >Set-Cookie - HTTP - MDN Web Docs
The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent...
Read more >API Reference - Express 4.x
You can add middleware and HTTP method routes (such as get , put , post , and so on) ... res , the...
Read more >Why isn't my session state working in ASP.NET Core? Session ...
The CheckConsentNeeded property is a predicate that is called by the framework to check whether non-essential cookies should be written to the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think for api routes, you should change the extension from
.tsx
to.ts
because nextjs will interpret your file as a react component, whereas it is an api route.Ah sorry, you can’t use
headers
andcookies
fromnext/navigation
in api routes, only in server components.You can have access to headers and cookies in your api routes, with the
req
object in your api handler :I made an example to test : https://stackblitz.com/edit/vercel-next-js-ze7x8r?file=pages%2Fapi%2Fcookies.ts,app%2Fpage.tsx