2

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?

winwiz1
  • 2,906
  • 11
  • 24
Leon_idas
  • 141
  • 12

1 Answers1

4

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.

May.D
  • 1,832
  • 1
  • 18
  • 34
MHM
  • 194
  • 1
  • 9