From 0cbde659d0b5ecebf4ecad0aaf64045c691fa81e Mon Sep 17 00:00:00 2001 From: DimaGashko Date: Wed, 5 Mar 2025 16:01:25 +0200 Subject: [PATCH] send content type only if data is passed --- src/utils/api.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/utils/api.ts b/src/utils/api.ts index ad1b064..88f7149 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -10,12 +10,17 @@ type FetchApiOptions = { } export async function fetchApi(path: string, method: string = 'GET', options: FetchApiOptions = {}) { + const headers = {} as { [key: string]: string }; + if (options.data) { + headers['Content-Type'] = 'application/json'; + } + try { const resp = await fetch(`${import.meta.env.VITE_API}${path}`, { ...options.requestInit, body: JSON.stringify(options.data), headers: { - 'Content-Type': 'application/json', + ...headers, ...options.requestInit?.headers, }, method,