Search code examples
javascriptcookiesaxioshttp-headershttp-post

how to tell axios to exclude cookie in post request


I tried to overwrite Cookie and set withCredentials:false, but that didn't work:


const request = await axios({
    url: '/api/payments',
    method: "post",
    data: payload,

    headers: {
      "Content-Type": "multipart/form-data",
      "Cookie": "access_token=''",
    },

    // auth token as  httponly cookie
    withCredentials: false,
  })

Solution

  • Axios is simply a wrapper for the browser's XMLHttpRequest functionality, which handles cookies automatically, so there's no way to override that behavior.

    As far as the withCredentials flag, this would only apply in the context of cross origin requests. Can read more here -

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials