Jonathan COHEN
Jonathan COHEN

Reputation: 125

SET WithCredentials globally with axios on VueJS

In main.js, I put

axios.defaults.withCredentials = true;

It should work, but by this way the cookies are not sent to the back end.

Here the request header

enter image description here

If in specific request I add {withCredentials: true}, it's work fine

However, I put an interceptor for all request, to check the options sent,

Do you have an idea not to put in each request this option?

Thank you in advance

enter image description here

Upvotes: 2

Views: 14802

Answers (2)

Alisson Reinaldo Silva
Alisson Reinaldo Silva

Reputation: 10715

Axios also provide interceptors. You can configure it like this:

axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    config.withCredentials = true;
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

Upvotes: 14

Jonathan COHEN
Jonathan COHEN

Reputation: 125

Finally, I decided to give up axios, and to use vue-resource, I used an interceptor to add credential = true, like this, and it works fine.

In the main.js file :

Vue.http.interceptors.push(function(request) {
  request.credentials = true
  return function(response) {
  };
});

Upvotes: -2

Related Questions