fix: remove forced multipart/form-data placeholder header in postForm/putForm/patchForm - #10980
fix: remove forced multipart/form-data placeholder header in postForm/putForm/patchForm#10980ErnestHysa wants to merge 1 commit into
Conversation
…r in postForm/putForm/patchForm The placeholder Content-Type header prevented the adapter from setting the proper Content-Type with boundary. The adapter now correctly handles it.
Ap-0007
left a comment
There was a problem hiding this comment.
Thanks for submitting this fix!
While removing the forced 'Content-Type': 'multipart/form-data' placeholder from postForm/putForm/patchForm solves the issue of sending bare headers in React Native Android, doing so unfortunately breaks auto-serialization for plain JavaScript object payloads.
The Problem
Axios's default request transformer in lib/defaults/index.js relies on detecting 'multipart/form-data' in the Content-Type header to trigger the serialization of plain objects to FormData:
if ((isFileList = utils.isFileList(data)) || contentType.indexOf('multipart/form-data') > -1) {
// serializes plain object payload to FormData
return toFormData(...);
}If we completely remove the placeholder header, contentType will be empty, and the plain object will fall through to standard JSON serialization (producing Content-Type: application/json).
Suggested Solution
The root cause for React Native Android is that resolveConfig.js only clears the Content-Type header if platform.hasStandardBrowserEnv is true. Since React Native excludes standard browser environment flags but uses standard FormData without getHeaders(), the placeholder is never cleared.
Instead of removing the placeholder header from the helpers, we can update lib/helpers/resolveConfig.js to clear the Content-Type header for any standard FormData object (i.e. those without .getHeaders()):
if (utils.isFormData(data)) {
if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv || !utils.isFunction(data.getHeaders)) {
headers.setContentType(undefined); // Let browser/RN runtime handle it and append the boundary
} else if (utils.isFunction(data.getHeaders)) {
// Node.js FormData (like form-data package)
// ...This ensures:
postFormauto-serialization continues to work by keeping the placeholder header during request transformation.- The placeholder header is correctly stripped before the adapter runs in standard environments and React Native, allowing the runtime to generate the proper boundary.
|
The PR is superseded by the already-merged React Native FormData fix in #10898, and this diff introduces a public behaviour regression. |
Fixes axios/axios #10886.
Removes the forced bare
Content-Type: multipart/form-dataplaceholder header inpostForm/putForm/patchFormhelper methods. The adapter now correctly sets the Content-Type with the proper boundary.Summary by cubic
Removes the forced multipart/form-data placeholder header from form helpers so the adapter sets the correct Content-Type with boundary. Fixes #10886 and ensures proper multipart uploads.
Description
Content-Type: multipart/form-datainpostForm/putForm/patchForm.multipart/form-data; boundary=....Docs
/docs/to clarify:Content-Typemanually for FormData;axiossets it with the correct boundary.postForm/putForm/patchFormshould omit manual headers.Testing
multipart/form-data; boundary=...when using FormData with helpers.multipart/form-dataheader is sent by default.Content-Type(if set) is respected.Semantic version impact
Written for commit 56edece. Summary will update on new commits.