axios: 'Content-Type': 'multipart/form-data' Can not work normally

I use {‘Content-Type’: ‘multipart / form-data’} to upload files but not work I use the following code:

let formBox = new FormData();
formBox.append('file', files[0]);
axios({
	method: 'post',
	url: api.pushZipFile,
	data: formBox,
	headers: {'Content-Type': 'multipart/form-data'}
})

Return the following results: image image

I refer to Issues to find a solution, but it did not succeed. https://github.com/mzabriskie/axios/issues/318

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15

Most upvoted comments

I get the same ‘[object Object]’ string in the payload. Anyone figure this out?

Got into the same problem. Applying your workaround, do you also see as request payload in chrome [object Object]?

@CliveTeow Like i said, you can’t inspect file uploads on the client side… It’s something you’ll have to diagnose/test on the server side exclusively. You have to disable network inspection with your debugger for it to work. It’s a limitation of some sort not with axios but with the debugging tools.

I believe this may just be because of using a debugger with Network Inspect enabled. Just some sort of limitation with file uploads. Try turning Network Inspect off (or the debugger off).

This problem has been resolved. Problem Cause: When I pass the default parameters to create the instance, my FormData object is converted to an empty object.

let configDefault = () => {
  return {
    method: 'post',
    url: '',
    responseType: 'json',
    data: {},
    params: {},
    transformRequest: [function (data) {
      return data;
    }],
    transformResponse: [function (data) {
      return data;
    }]
  }
}
Axios.create(defaultConfig())

Workaround: When creating an instance, the default parameters are not passed.

Axios.create()

I do not know why this is so, perhaps, only I am a person to encounter this problem