From 207a0862045342c104882b6aa5a842c0063bb141 Mon Sep 17 00:00:00 2001 From: Phil Brown Date: Wed, 27 Apr 2022 10:03:29 +1000 Subject: [PATCH] Fixing FormData content-type handling in XHR Removes the content-type request header if the request body is FormData and the request is made from a standard browser environment --- lib/adapters/xhr.js | 8 ++++++-- test/specs/headers.spec.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index 5da074dde9..c0822bf796 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -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 diff --git a/test/specs/headers.spec.js b/test/specs/headers.spec.js index c1d10d7b89..04f4bbcf37 100644 --- a/test/specs/headers.spec.js +++ b/test/specs/headers.spec.js @@ -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(); + }) + }) });