From 5ede024b4cad7422e20d2884a6c3cb3113a6eeca Mon Sep 17 00:00:00 2001 From: Steve Korshakov Date: Wed, 27 Aug 2025 00:27:32 -0700 Subject: [PATCH] fix: handle empty JSON bodies in content type parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix "Unexpected end of JSON input" error by: - Check for empty/whitespace-only body before parsing - Return empty object for empty bodies instead of failing - Add detailed error logging for debugging JSON parse failures - Prevent crashes on malformed or empty request bodies 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- sources/app/api.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sources/app/api.ts b/sources/app/api.ts index c7832aa..6489d51 100644 --- a/sources/app/api.ts +++ b/sources/app/api.ts @@ -97,11 +97,21 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }> { parseAs: 'string' }, function (req, body, done) { try { - const json = JSON.parse(body as string); + const bodyStr = body as string; + + // Handle empty body case + if (!bodyStr || bodyStr.trim() === '') { + (req as any).rawBody = bodyStr; + done(null, {}); + return; + } + + const json = JSON.parse(bodyStr); // Store raw body for webhook signature verification - (req as any).rawBody = body; + (req as any).rawBody = bodyStr; done(null, json); } catch (err: any) { + log({ module: 'content-parser', level: 'error' }, `JSON parse error: ${err.message}, body: "${body}"`); err.statusCode = 400; done(err, undefined); }