0

I am trying to make a post request which looks like this

axios
        .post(`http://127.0.0.1:8000/api/create/${this.props.id}`, {
          headers: {
            Authorization: `Token ${token}`
          },
          xsrfCookieName: "XSRF-TOKEN",
          xsrfHeaderName: "X-CSRFToken"
        })
        .then();

I have added essential things in settings.py also, such as CSRF_COOKIE_NAME = "XSRF-TOKEN"

I also have

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',


    ),
}
Abhijit Ghate
  • 382
  • 3
  • 16

1 Answers1

0

You may need to add ensure_csrf_cookie in your code.

A page makes a POST request via AJAX, and the page does not have an HTML form with a csrf_token that would cause the required CSRF cookie to be sent.

from django.views.decorators.csrf import ensure_csrf_cookie
 @ensure_csrf_cookie

Read more about ensure_csrf_cookie. Let me know if that helps.

Steve Cahn
  • 13
  • 3