skip to Main Content

Tried adding {withCredentials: true} but still unable to send cookie to backend

return await axios.post(`${API_URL}/posts/category/${id}`, {
  withCredentials: true
})

enter image description here

Cookie

enter image description here

2

Answers


  1. I found this answer on the Internet, hope it will help you.
    Since 2020, Chrome add more annoying restricts to cross domain cookies settings, you must set cookies with SameSite to none, otherwise Chrome will refuse to send cookies. More, if you set SameSite, you must set secure.

    proxy_cookie_path / "/; secure; SameSite=none";
    
    Login or Signup to reply.
  2. The issue is that you’re sending the { withCredentials: true } object as the 2nd parameter to axios.post(). The signature for that function is

    axios.post(url[, data[, config]])

    Typically with a POST request you’ll want to send some data but if you really want to skip it, pass undefined as the 2nd parameter

    return axios.post(`/posts/category/${id}`, undefined, {
      baseURL: API_URL,
      withCredentials: true,
    });
    

    I highly recommend taking advantage of Axios’ ability to create customised instances with convenient defaults

    const apiClient = axios.create({
      baseURL: API_URL,
      withCredentials: true,
    });
    
    // ...
    
    return apiClient.post(`/posts/category/${id}`); // much easier
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search