Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed FormData posting in browser environment by reverting #3785; #4640

Merged
merged 4 commits into from Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/adapters/xhr.js
Expand Up @@ -27,6 +27,10 @@ module.exports = function xhrAdapter(config) {
}
}

if (utils.isFormData(requestData) && utils.isStandardBrowserEnv()) {
delete requestHeaders['Content-Type']; // Let the browser set it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be done further down in order to use normalised headers?

Using this here would only catch the specific casing Content-Type and not content-type

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably yes, but this PR only brings back the removed code and adds an additional environment check to fix the regression bug. Axios headers helper is far away from perfection, and it should be deeply refactored to be really caseless, not just locally.
Unfortunately, the PR #4215 where the headers helper is reworked is still not merged, so it would be better to keep it as it was for a while.
https://github.com/axios/axios/pull/4215/files#diff-e3e3dd5daf3f63345305a7397f39f6adce58f3ff9b987a96da808b060079958b

}

var request = new XMLHttpRequest();

// HTTP basic authentication
Expand Down
14 changes: 14 additions & 0 deletions test/specs/formdata.spec.js
@@ -0,0 +1,14 @@

describe('FormData', function() {
it('should allow FormData posting', function () {
return axios.postForm('http://httpbin.org/post', {
a: 'foo',
b: 'bar'
}).then(({data}) => {
expect(data.form).toEqual({
a: 'foo',
b: 'bar'
});
});
});
})