[Django]-How to attach csrf header and value in axios POST request

2👍

  let csrftoken = Vue.$cookies.get('vcubes')

  return axios.post('', data, {
    headers: {
      'X-CSRFToken': csrftoken
    }
  }).then(response => {
    return response.data
  })

This is pretty similar to Shane’s answer, but show’s how to get the csrf token in Vue. If your problem is getting the cookie, then this SO answer should help you.

1👍

Here’s what I use in django:

result = { "insert your key values here" }
data = JSON.stringify(result)
axios.post("{% url 'error_checking' %}", data, {headers: {'X-CSRFToken': '{{ csrf_token }}',}})

Leave a comment