question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cookies on behaving as expected on API routes

See original GitHub issue

Verify 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:open
  • Created a year ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

5reactions
Fredkiss3commented, Nov 4, 2022

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.

2reactions
Fredkiss3commented, Nov 28, 2022

Ah sorry, you can’t use headers and cookies from next/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 :

import type { NextApiRequest, NextApiResponse } from 'next';


export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  const headers = req.headers;
  const cookies = req.cookies;

  return res.status(200).json({ headers, cookies });
}

I made an example to test : https://stackblitz.com/edit/vercel-next-js-ze7x8r?file=pages%2Fapi%2Fcookies.ts,app%2Fpage.tsx

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found