From f945b28bde530a8c18c15987a1517aa9df76d9b8 Mon Sep 17 00:00:00 2001 From: Johnny Hausman Date: Fri, 8 Dec 2023 20:44:50 -0600 Subject: [PATCH 1/4] [fix] Prevent setting Content-Type header if the provided data is FormData --- src/js/atomic/atomic.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/js/atomic/atomic.js b/src/js/atomic/atomic.js index 66815a6..67a6af1 100644 --- a/src/js/atomic/atomic.js +++ b/src/js/atomic/atomic.js @@ -97,6 +97,10 @@ return {data: result, xhr: req}; }; + var isFormData = function(obj) { + return Object.prototype.toString.call(obj) === '[object FormData]'; + } + /** * Convert an object into a query string * @link https://blog.garstasio.com/you-dont-need-jquery/ajax/ @@ -106,7 +110,7 @@ var param = function (obj) { // If already a string, or if a FormData object, return it as-is - if (typeof (obj) === 'string' || Object.prototype.toString.call(obj) === '[object FormData]') return obj; + if (typeof (obj) === 'string' || isFormData(obj)) return obj; // If the content-type is set to JSON, stringify the JSON object if (/application\/json/i.test(settings.headers['Content-type']) || Object.prototype.toString.call(obj) === '[object Array]') return JSON.stringify(obj); @@ -214,6 +218,11 @@ // Merge options into defaults settings = extend(defaults, options || {}); + // clear Content-Type if provided data is FormData + if (isFormData(settings.data)) { + delete settings.headers['Content-type']; + } + // Make request return makeRequest(url); From 3d224ea3262f19a742bd73e56285d749e1374c7c Mon Sep 17 00:00:00 2001 From: Johnny Hausman Date: Fri, 8 Dec 2023 20:52:29 -0600 Subject: [PATCH 2/4] [fix] added documentation for isFormData() --- src/js/atomic/atomic.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/js/atomic/atomic.js b/src/js/atomic/atomic.js index 67a6af1..f362a35 100644 --- a/src/js/atomic/atomic.js +++ b/src/js/atomic/atomic.js @@ -78,6 +78,16 @@ }; + /** + * Check if the provided obj is a FormData object + * @private + * @param {Object|Array|String} obj The object + * @return {Bool} + */ + var isFormData = function(obj) { + return Object.prototype.toString.call(obj) === '[object FormData]'; + }; + /** * Parse text response into JSON * @private @@ -97,10 +107,6 @@ return {data: result, xhr: req}; }; - var isFormData = function(obj) { - return Object.prototype.toString.call(obj) === '[object FormData]'; - } - /** * Convert an object into a query string * @link https://blog.garstasio.com/you-dont-need-jquery/ajax/ From 1808354b5d9ab57577f7cfd1fd3a7cfb5dbac07a Mon Sep 17 00:00:00 2001 From: Johnny Hausman Date: Fri, 8 Dec 2023 21:08:12 -0600 Subject: [PATCH 3/4] [add] added test for FormData --- test/spec/atomic-spec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/spec/atomic-spec.js b/test/spec/atomic-spec.js index ce8253a..75fd131 100644 --- a/test/spec/atomic-spec.js +++ b/test/spec/atomic-spec.js @@ -95,6 +95,19 @@ describe('atomic', function () { .toHaveBeenCalledWith('Content-type', 'application/json'); }); + it('should not set Content-type when using FormData', function() { + atomic.ajax({ + url: '/endpoint', + headers: { + 'Content-type': 'application/json' + }, + data: new FormData() + }); + + expect(XMLHttpRequest.prototype.setRequestHeader) + .not.toHaveBeenCalledWith('Content-type', 'application/json'); + }); + }); }); \ No newline at end of file From 180c7dce6bf9dde0fefbfc145dfc1c51ff268be5 Mon Sep 17 00:00:00 2001 From: Johnny Hausman Date: Fri, 8 Dec 2023 21:09:52 -0600 Subject: [PATCH 4/4] [fix] condition statement --- test/spec/atomic-spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec/atomic-spec.js b/test/spec/atomic-spec.js index 75fd131..a99bf83 100644 --- a/test/spec/atomic-spec.js +++ b/test/spec/atomic-spec.js @@ -95,7 +95,7 @@ describe('atomic', function () { .toHaveBeenCalledWith('Content-type', 'application/json'); }); - it('should not set Content-type when using FormData', function() { + it('should not be set when using FormData', function() { atomic.ajax({ url: '/endpoint', headers: {