I've developed an Django file upload API which receive the posted data from client and save the data as file.
According to the Django CSRF manual, the HTTP request header should set X-CSRFToken with the csrftoken cookie value. I've set the X-CSRFToken by the code below, but the POST request still forbidden(403) by Django server as the picture below shows.
$(document).ready(function(){
var authid
$.get("http://localhost:8000/v1/getAuthID?username=testuser1&password=123", function(data){
authid = data["authid"];
var csrftoken = $.cookie('csrftoken');
console.log(csrftoken);
$.ajaxSetup({
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
});
url = "http://localhost:8000/v1/file".replace("{authid}", authid).replace("{token}", csrftoken)
$.post(url, function(data){
})
})
})
How did you overcome the Django CSRF by send POST request to Django server?
Thanks!