4

Integrate Axios with Django Rest Framework

 2 years ago
source link: https://www.guguweb.com/2021/06/12/integrate-axios-with-django-rest-framework/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Integrate Axios with Django Rest Framework

Do you need to integrate the Axios HTTP client with Django Rest Framework? Then make sure to correctly configure the Django built-in Cross Site Request Forgery protection.

TLDR: add these variables to your Django settings:

CSRF_COOKIE_NAME = 'XSRF-TOKEN'
CSRF_HEADER_NAME = 'HTTP_X_XSRF_TOKEN'

Axios has built-in support for CSRF protection, and this is the default configuration:

// name of the cookie to use as a value for xsrf token
xsrfCookieName: 'XSRF-TOKEN'

// name of the http header that carries the xsrf token value
xsrfHeaderName: 'X-XSRF-TOKEN'

According to Django documentation:

"As with other HTTP headers in request.META, the header name received from the server is normalized by converting all characters to uppercase, replacing any hyphens with underscores, and adding an 'HTTP_' prefix to the name. For example, if your client sends a 'X-XSRF-TOKEN' header, the setting should be 'HTTP_X_XSRF_TOKEN'."


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK