I in my React app I send a request containing a JSON body with various fields, among which is an array of File
type images.
const body = {
title: 'title',
price: 100,
images: [...]
};
axios.post(url, body);
This doesn't work and the images
array arrives at the Express app empty.
I have consulted the internet and have tried using FormData
. However, all the solutions I saw (example) only have images in them, i.e.
const images = [...];
const data = new FormData();
images.forEach(img => data.append(img.title, img));
But I want the entire thing - the text and the images, because this input is supposed to go into MongoDB.
I have tried simply putting the entire object in a FormData
:
const data = new FormData();
data.append('body', body);
But I must not understand how FormData
actually works because trying to iterate over the req.body
over in Express just yields nothing:
app.get((req, res) => {
const data = Object.fromEntries(req.body) // yields nothing
}
Any other attempts like using multer
reinforce that I'm doing something wrong, because I cannot for the life of me get a non-empty request body.
What I want to do is to be able to send a full HTTP body of data that includes text, numbers, and an array of images, such that over on the Express app I could save those images to disk. Any nice suggestions would be appreciated.