I want to send a post rquest with axios that has body data and form data and I can get anything to reach through to my route hadler:
axios({
method: 'POST',
url: `${keys.SERVER_URL}/post/new-post/${isAuthenticated()._id}`,
formData,
data: { title, content },
headers: {
'Content-Type': 'application/json',
'Content-Type': 'multipart/form-data',
},
});
then in the route handler I console.log and I get nothing:
app.post("/new-post", (req, res) => {
console.log(req.body);
console.log(req.file):
});
There is no formData option for the axios() options object. If you want to set formData, you set the data property to your formData.
So, that illustrates that there is ONLY one set of data that you send with a POST or a PUT. If you want to send multiple pieces of data, you have to combine them into the same set of data and you have to obviously make them the same type of data (since there is only one master content-type). If you really needed to send multiple different types of data, then you would need to send a multi-part body that has separate sections to it. You would likely need some sort of helper to create that multi-part body since that isn't something that axios will do for you and it can get a little complicated.
If you could describe what you're actually trying to do (what the actual purpose of this request is), then we might be able to advise how to combine your data into one simpler type so it an be sent more simply.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With