I've encountered a similar issue where the solutions provided didn't quite work for me:
Fetch, set-cookies and csrf
Proper Django CSRF validation using fetch post request
Even though I believe my post request content is correct, I still keep getting a 403 error.
const url = "/post/create"
let csrftoken = Cookies.get('csrftoken'); //using library
const headers = new Headers({
'X-CSRF-TOKEN': csrftoken
});
return fetch(url, {
method: 'POST',
headers,
credentials: 'same-origin',
mode: 'same-origin',
body: JSON.stringify({
content: content
})
});
Any thoughts on what might be causing this?
UPDATE: Here's the solution I managed to come up with
fetch(url, {
method: 'POST',
mode: "same-origin",
headers: {
"X-CSRFToken": csrftoken,
"Accept": "network/json",
"Content-Type": "network/json",
},
body: JSON.stringify({
content: content
})