From 7b7b149928fd0374e167b519d97a74c10026167c Mon Sep 17 00:00:00 2001 From: Samu Date: Fri, 4 Sep 2026 13:14:46 +0200 Subject: [PATCH] fix: bodyless posts no longer send json content-type (fastify 400 on empty body) --- web/src/api.ts | 5 +++-- web/test/api.test.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/web/src/api.ts b/web/src/api.ts index 35663dc..f244f9f 100644 --- a/web/src/api.ts +++ b/web/src/api.ts @@ -48,8 +48,9 @@ async function request(path: string, init?: RequestInit): Promise { function post(path: string, payload?: unknown): Promise { return request(path, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: payload === undefined ? undefined : JSON.stringify(payload), + ...(payload !== undefined + ? { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) } + : {}), }) } diff --git a/web/test/api.test.ts b/web/test/api.test.ts index 50caa56..98f9110 100644 --- a/web/test/api.test.ts +++ b/web/test/api.test.ts @@ -40,4 +40,19 @@ describe('api additions', () => { expect(err).toBeInstanceOf(ApiError) expect((err as ApiError).code).toBe('no_subsonic_config') }) + + it('bodyless posts do not send a json content-type (Fastify 400s on empty json bodies)', async () => { + fetchMock.mockClear() + fetchMock.mockImplementation(() => jsonOk({ ok: true })) + await api.returnLoan(7) + await api.markPlayed('a1') + await api.logout() + await api.triggerBackup() + for (const call of fetchMock.mock.calls) { + const init = call[1] as RequestInit | undefined + const headers = (init?.headers ?? {}) as Record + expect(headers['Content-Type']).toBeUndefined() + expect(init?.body).toBeUndefined() + } + }) })