From 97c3e59821837c816eabbd2af4a5d2b640df4527 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 19 Jul 2025 16:50:34 +0200 Subject: [PATCH] do not try to pass request body to get request --- frontend/src/helpers/fetcher.ts | 42 ++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/frontend/src/helpers/fetcher.ts b/frontend/src/helpers/fetcher.ts index 42cbf8fb9..a5dec59ac 100644 --- a/frontend/src/helpers/fetcher.ts +++ b/frontend/src/helpers/fetcher.ts @@ -102,17 +102,21 @@ class HttpClient { const url = this.buildUrl(config) const init: RequestInit = {method: config.method, headers: config.headers} - const body = this.prepareRequestBody(config.data, config.transformRequest) + // GET and HEAD requests cannot have a body + const method = config.method?.toUpperCase() + if (method !== 'GET' && method !== 'HEAD') { + const body = this.prepareRequestBody(config.data, config.transformRequest) - if (typeof body !== 'undefined') { - if (body instanceof FormData || body instanceof Blob) { - init.body = body as BodyInit - } else if (typeof body === 'string') { - init.body = body - } else { - init.body = JSON.stringify(body) - if (init.headers && !(init.headers as Record)['Content-Type']) { - (init.headers as Record)['Content-Type'] = 'application/json' + if (typeof body !== 'undefined') { + if (body instanceof FormData || body instanceof Blob) { + init.body = body as BodyInit + } else if (typeof body === 'string') { + init.body = body + } else { + init.body = JSON.stringify(body) + if (init.headers && !(init.headers as Record)['Content-Type']) { + (init.headers as Record)['Content-Type'] = 'application/json' + } } } } @@ -170,13 +174,19 @@ class HttpClient { reject(err) } - const body = this.prepareRequestBody(config.data, config.transformRequest) - if (body instanceof FormData || body instanceof Blob) { - xhr.send(body as BodyInit) - } else if (typeof body === 'string' || typeof body === 'undefined') { - xhr.send(body) + // GET and HEAD requests cannot have a body + const method = config.method?.toUpperCase() + if (method === 'GET' || method === 'HEAD') { + xhr.send() } else { - xhr.send(JSON.stringify(body)) + const body = this.prepareRequestBody(config.data, config.transformRequest) + if (body instanceof FormData || body instanceof Blob) { + xhr.send(body as BodyInit) + } else if (typeof body === 'string' || typeof body === 'undefined') { + xhr.send(body) + } else { + xhr.send(JSON.stringify(body)) + } } }) }