Next.js Discord

Discord Forum

API route has no cookies

Unanswered
Siamese Crocodile posted this in #help-forum
Open in Discord
Siamese CrocodileOP
I am making a fetch request from my middleware.ts file to an API route, when I try to get a cookie in my API route it is not there, however I see the cookie in the browser. Has anyone else seen this?

4 Replies

Serbian Hound
I have the exact same thing...so annoying
yet request.cookies is
 _headers: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(12) {
      'accept' => [Object],
      'accept-encoding' => [Object],
      'accept-language' => [Object],
      'cache-control' => [Object],
      'connection' => [Object],
      'host' => [Object],
      'sec-fetch-mode' => [Object],
      'user-agent' => [Object],
      'x-forwarded-for' => [Object],
      'x-forwarded-host' => [Object],
      'x-forwarded-port' => [Object],
      'x-forwarded-proto' => [Object]
    },
Serbian Hound
Ok I found a solution @Siamese Crocodile , your originating request must be from the client. so a client component or such, then it works
Example I did this and now it works:
   const response = await fetch("http://localhost:3000/api/cover", {
        next: {
            tags: ["cover"]
        },
        headers: headers(),
        cache: 'no-cache',
    });