axios: Can not change Contant-Type when data is FormData

axios({
                method: "PUT",
                url: url,
                transformRequest: [function (data, headers) {
                    // Do whatever you want to transform the data
                    return formData;
                }],
                headers: {
                    'Content-Type': file.type
                },
                "withCredentials": false
            });

Launched request with Content-Type: Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryPz13ZA77aLqgPlwB

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 9
  • Comments: 17 (4 by maintainers)

Most upvoted comments

One really ugly hack, without rewriting the app would be to hack object’s prototype. This will make Content-type stay as it is.

transformRequest: payload => {
       // ...
        Object.setPrototypeOf(formData, null);
        return formData;
      },

It’s because of this part https://github.com/axios/axios/blob/master/lib/adapters/xhr.js#L18-L20 Looks like it was first reported in 2017 https://github.com/axios/axios/issues/767 but was closed without being fixed

Removing this part worked for me

@ppozniak Unfortunately, I can’t change the behavior of axios. Finally I solved the problem by using XMLHttpRequest.