diff --git a/client/src/lib/api.ts b/client/src/lib/api.ts index aef23cd..ad2b1bd 100644 --- a/client/src/lib/api.ts +++ b/client/src/lib/api.ts @@ -118,12 +118,15 @@ export interface ListMeta { } async function request(path: string, init?: RequestInit): Promise { + const headers = new Headers(init?.headers); + // Only set JSON content-type when we actually send a body (DELETE/GET have none — + // Fastify rejects empty body + application/json). + if (init?.body && !(init.body instanceof FormData) && !headers.has("Content-Type")) { + headers.set("Content-Type", "application/json"); + } const res = await fetch(path, { ...init, - headers: { - ...(init?.body instanceof FormData ? {} : { "Content-Type": "application/json" }), - ...init?.headers, - }, + headers, }); if (!res.ok) { let msg = res.statusText; @@ -136,7 +139,10 @@ async function request(path: string, init?: RequestInit): Promise { throw new Error(msg || `HTTP ${res.status}`); } if (res.status === 204) return undefined as T; - return res.json() as Promise; + // Some DELETEs return empty body + const text = await res.text(); + if (!text) return undefined as T; + return JSON.parse(text) as T; } export const api = {