Convert image/jpeg, image/png, etc. files to multipart/form-data format node js

2.2k Views Asked by At

I have an application that gets random image and posts it to blog site. But to give a blog site an image it has to have a type of multipart/form-data.

  const FormData = require('form-data')
  const form = new FormData()

  const { data } = await axios.get(
    'https://photo-works.net/images/europe-landscape-photo-edited.jpg'
  ) // image/jpeg

  form.append('my-photo', data)
  const photo = await axios.post(url, { photo: form })   // that should be multipart/form-data format
1

There are 1 best solutions below

0
On BEST ANSWER

You should pass right header information as follows

var formData = new FormData();
formData.append("my-photo", data);
axios.post('upload_file', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})