0

I am using nest js and want to set the cookies when the user will hit a specific endpoint:

@Get()
setCookiesApi(@Res({ passthrough: true }) response:Response) {
  response.setCookie('key', 'value')
}

This code works and the cookies are set in cookies storage from the Application tab in Chrome. If i try setting cookies using post:

@Post()
setCookiesApi(@Res({ passthrough: true }) response:Response) {
  response.setCookie('key', 'value')
}

My code on UI:

  try {
      const response = await axios.post(
        `http://localhost:3000/api/v1/hello`,
        user,
        {
          method: 'post',
          headers: {
            withCredentials: true,
          },
          data: user,
        },
      );
      if (response.data) {
        // sss
      }
    } catch (err) {
      if (err instanceof AxiosError) {
        if (err.response) {
          toast.error(err.response.data.message);
        }
      }
    }

main.js file

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.setGlobalPrefix('api/v1');
  app.useGlobalPipes(new ValidationPipe());
  app.use(cookieParser());
  app.enableCors({ origin: 'http://127.0.0.1:5173', credentials: true });
  await app.listen(3000);
}
bootstrap();

...then the cookies storage is empty and no cookie is set.
Question: Why get request works but post not and how to solve this?

Asking
  • 3,487
  • 11
  • 51
  • 106
  • How do you make the request? – Konrad Dec 06 '22 at 19:45
  • @Konrad, i added a short code in my question. – Asking Dec 06 '22 at 19:50
  • Does this answer your question? [Make Axios send cookies in its requests automatically](https://stackoverflow.com/questions/43002444/make-axios-send-cookies-in-its-requests-automatically) – Konrad Dec 06 '22 at 19:52
  • @Konrad, do you know what could be the issue because i tried everything – Asking Dec 06 '22 at 20:10
  • Are you sure that you are hitting the correct endpoint? – Konrad Dec 06 '22 at 20:24
  • @Konrad yes, 100% – Asking Dec 07 '22 at 09:39
  • @Konrad, i am using Vite with react js, noticed that when in run my code using CRA where i have localhost, then all cookies are set, but when using vite with `http://127.0.0.1:5173/` url, the cookies are not set. Do the cookies work only with localhost? – Asking Dec 08 '22 at 09:40
  • Should work either way – Konrad Dec 08 '22 at 10:34
  • 1
    @Konrad, i changed the Vite url to `http://localhost:5173/` and it is working but not with `http://127.0.0.1:5173/` – Asking Dec 08 '22 at 11:19
  • 1
    This is unexpected, I didn't know that. You can ask another question like "Why cookies work for `localhost` but not for `127.0.0.1`?", you will probably gather more attention and get the answer – Konrad Dec 08 '22 at 12:01

2 Answers2

1

I think the GET request works because it is an HTTP method that allows for retrieving data from a server, and the setCookie() method is used to set a cookie in the response.

The POST request doesn't work because POST is typically used for creating or modifying data on a server, and the setCookie() method is not designed to handle data modifications.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
DMyers205
  • 1
  • 2
0

It works with POST method as well. Try something similar like this:

@Post()
setCookiesApi(@Res({ passthrough: true }) response:Response) {
  response.setHeader('Set-Cookie', cookie);

  return response.send();
}