From f4f4764644e6faf0c8705bf32e5006d1c7aaf12b Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Sat, 13 Feb 2021 21:52:48 +0300 Subject: [PATCH] Do not send Content-Type on file upload --- frontend/app/common/fetcher.test.ts | 6 +++--- frontend/app/common/fetcher.ts | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/frontend/app/common/fetcher.test.ts b/frontend/app/common/fetcher.test.ts index 4ee4acc7..ef7626bc 100644 --- a/frontend/app/common/fetcher.test.ts +++ b/frontend/app/common/fetcher.test.ts @@ -146,10 +146,10 @@ describe('fetcher', () => { body: JSON.stringify(data), }); }); - it('should send form data', async () => { + + it("shouldn't send content-type with form data", async () => { expect.assertions(1); - const headersWithMultipartData = { ...headers, 'Content-Type': 'multipart/form-data' }; const body = new FormData(); mockFetch(); @@ -158,7 +158,7 @@ describe('fetcher', () => { expect(window.fetch).toHaveBeenCalledWith(apiUrl, { method: 'post', body, - headers: headersWithMultipartData, + headers, }); }); }); diff --git a/frontend/app/common/fetcher.ts b/frontend/app/common/fetcher.ts index 71bc9aca..c77412a2 100644 --- a/frontend/app/common/fetcher.ts +++ b/frontend/app/common/fetcher.ts @@ -48,7 +48,8 @@ const createFetcher = (baseUrl: string = ''): Methods => { headers[XSRF_HEADER] = getCookie(XSRF_COOKIE) || ''; if (body instanceof FormData) { - headers['Content-Type'] = 'multipart/form-data'; + // Shouldn't add any kind of `Content-Type` if we send `FormData` + // Now FormData is sent only in case of uploading file params.body = body; } else if (typeof body === 'object' && body !== null) { headers['Content-Type'] = 'application/json';