1
0

fix: bodyless posts no longer send json content-type (fastify 400 on empty body)

This commit is contained in:
2026-09-04 13:14:46 +02:00
parent 3edf7574f9
commit 7b7b149928
2 changed files with 18 additions and 2 deletions

View File

@@ -48,8 +48,9 @@ async function request<T>(path: string, init?: RequestInit): Promise<T> {
function post<T>(path: string, payload?: unknown): Promise<T> { function post<T>(path: string, payload?: unknown): Promise<T> {
return request<T>(path, { return request<T>(path, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, ...(payload !== undefined
body: payload === undefined ? undefined : JSON.stringify(payload), ? { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }
: {}),
}) })
} }

View File

@@ -40,4 +40,19 @@ describe('api additions', () => {
expect(err).toBeInstanceOf(ApiError) expect(err).toBeInstanceOf(ApiError)
expect((err as ApiError).code).toBe('no_subsonic_config') 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<string, string>
expect(headers['Content-Type']).toBeUndefined()
expect(init?.body).toBeUndefined()
}
})
}) })