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

Fixing FormData content-type handling in XHR #4642

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 6 additions & 2 deletions lib/adapters/xhr.js
Expand Up @@ -151,8 +151,12 @@ module.exports = function xhrAdapter(config) {
// Add headers to the request
if ('setRequestHeader' in request) {
utils.forEach(requestHeaders, function setRequestHeader(val, key) {
if (typeof requestData === 'undefined' && key.toLowerCase() === 'content-type') {
// Remove Content-Type if data is undefined
if (
key.toLowerCase() === 'content-type' &&
(typeof requestData === 'undefined' ||
(utils.isFormData(requestData) && utils.isStandardBrowserEnv()))
) {
// Remove Content-Type if data is undefined or FormData in a browser environment
delete requestHeaders[key];
} else {
// Otherwise add header to the request
Expand Down
13 changes: 13 additions & 0 deletions test/specs/headers.spec.js
Expand Up @@ -112,4 +112,17 @@ describe('headers', function () {
done();
});
});

it('should remove content-type if data is FormData', function (done) {
axios.post('/foo', new FormData(), {
headers: {
'Content-Type': 'multipart/form-data'
}
});

getAjaxRequest().then(function (request) {
testHeaderValue(request.requestHeaders, 'Content-Type', undefined);
done();
})
})
});