I created simple API in Django and I need to fetch it with JavaScript, I get following error: Forbidden (CSRF token missing.): URL(placeholder instead of real url)
fetch(`/Post/${content[i].id}`, {
method: "POST",
}).then((data) => {
console.log(data);
})
How can I include token in API call?
There is an example and solution for your problem in Django official documentation.
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) {
});
I hope it could help you.