I have a react app for frontend and Django for backend. I am currently running the django app in the remote server and if I do POST request on it from Postman or from localhost it works fine. But when I deployed react app on the server and do a POST request, I get 404 error. Does anyone knows why that happens?
Edit: Here is how i am sending my post request to django with axios.
const handleSend = () =>{
const message = {
name:name,
email:email,
description: description
}
axios.post("api/messages/", message).then((res) => {
if(res.statusText === "Created"){
setPage("thankyou");
}
});
}
I have a proxy set up in the package.json file as (Example.com is the django server currently running in my apache server.)
"proxy": "http://example.com",
After hours of research and debugging errors I was finally able to send HTTP request from React to Django on the web server.
I was sending POST request from HTTPS to HTTP which caused the 404 error, so I added an SSL certificate for Django and I also used XMLHttpRequest instead of Axios. Here's my modified handleSend function. Hope this helps someone in the future.
const handleSend = () => {
const message = {
name: name,
email: email,
description: description,
};
function reqListener() {
if (req.status === 201) {
setPage("thankyou");
}
}
var req = new XMLHttpRequest();
req.addEventListener("load", reqListener);
req.open("POST", "https://example.com/api/messages/", true);
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
req.send(JSON.stringify(message));
};