[Vuejs]-Error: Request failed with code 403 in axios post api

0👍

You can add CSRF TOKEN in your axios call by this way :

Let’s say you’ve got the value of the token stored in a variable called csrfToken.

methods: {

  async loginWDiscord() {
  // Get the csrf-token
  csrfToken = "{% csrf_token %}";
  post_url = 'http://127.0.0.1:8000/api/v1/oauth2/login/';

  await axios(
      {
          method: 'post',
          url: post_url,
          data: {  // Change the datas to post for suit your needs
              username: 'demo',
              password: 'MyG00dyP4ss'
          },
          headers: {"X-CSRFToken": csrfToken},  // Here you pass the token
      })
      .then(response => {
          console.log(response)              
      })
      .catch(error => {
          console.log(error)
      })

  }

},

Leave a comment