I am using React + Django and trying to make a post request using axios but it is failing due to csrf. I have tried all the answers posted on internet to similar problem but none of them are working for some weird reason.
//Django View
def createUser(request):
username = request.POST['username']
email = request.POST['email']
resp = {
'username' : username,
'email' : email
}
return JsonResponse(resp)
//Axios Post
axios.post('http://localhost:8000/api/createUser/',{
username : 'xyz',
email : 'xyz@gmail.com'
},
{
headers: {
Content-Type': 'application/json',
}
});
tried adding defaults
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
axios.defaults.xsrfCookieName = "csrftoken";
but still getting csrf failed. I know i can do @csrf_exempt on the view but i want to keep the csrf check.