Почему токен csrf должен быть помещен в тело POST запроса, а не в заголовки в Django Rest Framework?

Я хотел бы узнать причину, по которой мы должны поместить токен csrf в body для POST-запросов (ключ csrfmiddlewaretoken) и в headers для остальных (ключ X-CSRFToken)?

You can use the header in case of a POST request as well. Indeed, this is often done for POST requests with AJAX (and other requests with side-effects). This is demonstrated in the Setting the token on the AJAX request section of the documentation [Django-doc]:

Наконец, вам нужно установить заголовок для вашего AJAX-запроса. Используя fetch() API:

const request = new Request(
    /* URL */,
    {
        method: 'POST',
        headers: {'X-CSRFToken': csrftoken},
        mode: 'same-origin' // Do not send CSRF token to another domain.
    }
);
fetch(request).then(function(response) {
    // ...
});
Вернуться на верх