[Vuejs]-Why does Axios not working with FormData?

1๐Ÿ‘

Iโ€™ve figured it out โ€“ Iโ€™m using snakecaseKeys package to manipulate the data before sending. It seems not to play well with FormData objects.

import snakecaseKeys from 'snakecase-keys'
axios.defaults.transformRequest = [(data) => {
    if (data) {
        return snakecaseKeys(data, { deep: true })
    }
}, ...axios.defaults.transformRequest]

Thanks for the comments guys!

0๐Ÿ‘

In case anyone else runs into this issue, you can side-step the transformations in you transformRequest using instanceof.

  transformRequest: [
    (data) => {
      if (data instanceof FormData) return data;
      // otherwise, transform your data
      return transformedData;
    },
  ],

-2๐Ÿ‘

in React the form should also have encType="multipart/form-data" attribute, may be in vue also?

Leave a comment