3

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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.