Make Axios send cookies in its requests automatically Make Axios send cookies in its requests automatically express express

Make Axios send cookies in its requests automatically


I had the same problem and fixed it by using the withCredentials property.

XMLHttpRequest from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request.

axios.get('some api url', {withCredentials: true});


TL;DR:

{ withCredentials: true } or axios.defaults.withCredentials = true


From the axios documentation

withCredentials: false, // default

withCredentials indicates whether or not cross-site Access-Control requests should be made using credentials

If you pass { withCredentials: true } with your request it should work.

A better way would be setting withCredentials as true in axios.defaults

axios.defaults.withCredentials = true


It's also important to set the necessary headers in the express response. These are those which worked for me:

app.use(function(req, res, next) {  res.header('Access-Control-Allow-Origin', yourExactHostname);  res.header('Access-Control-Allow-Credentials', true);  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');  next();});