From c36a606fe0eb090b40ee68e8176bc6cdbdcfea9b Mon Sep 17 00:00:00 2001 From: Ido Shamun Date: Fri, 3 Jul 2020 16:31:37 +0300 Subject: [PATCH 1/2] fix stringifying form data as json FormData type is `object` which caused redaxios to transform it into a string before sending the request. This made it not possible to send FormData at all through redaxios. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 792f758..fad8836 100644 --- a/src/index.js +++ b/src/index.js @@ -146,7 +146,7 @@ export default (function create(defaults) { const fetchFunc = options.fetch || fetch; const customHeaders = {}; - if (data && typeof data === 'object') { + if (data && typeof data === 'object' && !(data instanceof FormData)) { data = JSON.stringify(data); customHeaders['Content-Type'] = 'application/json'; } From 86fef6cb9e276606e3c9ec1b22d7fbbbdc95023a Mon Sep 17 00:00:00 2001 From: Ido Shamun Date: Fri, 3 Jul 2020 16:35:04 +0300 Subject: [PATCH 2/2] add test to cover form data request --- test/index.test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/index.test.js b/test/index.test.js index 8327ba5..6bb3361 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -75,6 +75,30 @@ describe('redaxios', () => { } }); + it('should issue POST requests with FormData', async () => { + const oldFetch = window.fetch; + try { + window.fetch = jasmine.createSpy('fetch').and.returnValue(Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve('yep') })); + const data = new FormData(); + formData.append('hello', 'world'); + const req = axios.post('/foo', formData, { headers: { 'content-type': 'multipart/form-data' } }); + expect(window.fetch).toHaveBeenCalledTimes(1); + expect(window.fetch).toHaveBeenCalledWith('/foo', jasmine.objectContaining({ + method: 'post', + headers: { + 'content-type': 'multipart/form-data' + }, + body: data + })); + const res = await req; + expect(res.status).toEqual(200); + expect(res.data).toEqual('yep'); + } + finally { + window.fetch = oldFetch; + } + }); + it('should accept a custom fetch implementation', async () => { const req = axios.get(jsonExample, { fetch }); expect(req).toBeInstanceOf(Promise);