1

I have an express server set up like this:

const express = require("express");
const app = express();
const cors = require("cors");
const cookieParser = require("cookie-parser");

app.use(express.json());
app.use(cors());
app.use(cookieParser());

app.post("/", (req, res) => {
  console.log(req.cookies)
  res.send();
});

app.listen(5000);

And the output of this is an empty object, but in my client-side (that is making the request), if I log document.cookie it prints a string with many values. Any idea why the same is not showing up in the server log?

EDIT`: the exact log is [Object: null prototype] {}

This is the request:

const login = async () => {
  const resp = await fetch("http://localhost:5000", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({msg: "example"}),
  });
  
};

login();
Josh Merrian
  • 291
  • 3
  • 11

1 Answers1

0

I had the exact same problem as you. But I was using the useSWR hooks in ReactJS and found the solution thanks to that post : How to send headers using swr.

Try your code with the "credentials" : include in your request like this:

const login = async () => {
  const resp = await fetch("http://localhost:5000", {
    method: "POST",
    headers: {
    "Content-Type": "application/json",
    },
    "credentials" : include,
    body: JSON.stringify({msg: "example"}),
  });
  
};

login();
Millcat
  • 1
  • 1