0

I am trying to send a number of data including an image to a node js server so that I can create a new document and store the image in the file system. I know that I should send a form data after appending the image to the backend and in the backend I should use multer package to read the form data and store the image. This I have implemented and works just fine.

The problem I am facing is that I have not been able to find a good way of sending form data alongside additional data, that are required for creating the new document in a single request. For that I first tried something like this {...data, ...formData} but it didn't work. Then I tried appending all the additional data inside the form data itself.

formData.append("basicDetails", data);
formData.append("picture", picture);

const res = await axios.post(BASE_URL, formData, {
    headers: {
        'Authorization': `Bearer ${token}`
    }
});

But now, since all the data is in form data, reading the data like req.body.basicDetails and trying the create a new document seems to not work. If I am not mistaken I need to use packages like multer to read form data. But is there a way to just use form data for picture and not for other data without making multiple requests?

samman adhikari
  • 571
  • 1
  • 7
  • 17

1 Answers1

0

I was researching a bit and I found this StackOverflow thread talking about formdata.

They were mentioning the headers, so add:

'Content-Type': 'multipart/form-data'

to your headers and try that.

Hope it works then :)