I have a javascript fetch API call made to a Django backend server
const res = fetch(url, {
credentials: 'include',
method: 'POST',
mode: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken
},
body: JSON.stringify(requestBody),
})
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
If I output the requestBody on the console before using it in the request, it looks like follows
{"add-Choice":["L"],"add-Left":["1"],"add-Right":["1"],"add-Value":["100"],"add-match":["8"],"user":["siteuser1"],"csrfmiddlewaretoken":["3K89ZvofjhIL2nZQoCcxphjmXborujsaLPn2FzlYiVmiBLODWhQQiAB5BhSXkQcF"],"add":["Submit"]}
Here string "add-" is used as a form prefix on the Django side view post methode. However, on the server view side, if I print the request.POST as follow
def post(self, request, *args, **kwargs):
print('Post request: ', request.POST)
I get the output as an empty QueryDict as follow
Post request: <QueryDict: {}>
Internal Server Error: /matchoffers/8
The request is being sent to the correct URL and is picked up the correct view-post methode. But it fails because the request is empty.