Sachin Gadhari
Sachin Gadhari

Reputation: 51

csrf token issue from Vuex to Django API backend

how to pass csrf token from vuex axios to Django API using a global interceptor in vuex. Currently, I am passing csrf with each post, put and delete request like

putResponseResults: function (context, postData) {
      axios.post('/api/responses/', postData,  { headers: { 'X-CSRFToken': Cookie.get('csrftoken')} })
        .then(function (response) {
          // console.log(response.data)
        })

Is there any global way to pass csrf token using vuex, so I don't need to write it every time.

Upvotes: 1

Views: 521

Answers (1)

antonku
antonku

Reputation: 7665

You can set a CSRF token as a default header (so that it will be attached to every subsequent request) as follows:

axios.defaults.headers.common['X-CSRFToken'] = 'your token';

More information on axios config defaults can be found in axios documentation.

Upvotes: 1

Related Questions