fix: properly handle empty bodies for DELETE/GET requests

Improve content type parser to handle HTTP methods correctly:
- DELETE and GET requests with empty bodies return undefined (no parsing)
- Other HTTP methods with empty bodies return empty object
- Add method and URL to error logging for better debugging
- Prevents JSON parsing errors on legitimate empty DELETE requests

Fixes the GitHub disconnect endpoint which uses DELETE without body.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Korshakov 2025-08-27 00:28:43 -07:00
parent 5ede024b4c
commit 0f222cfd98

View File

@ -99,9 +99,15 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
try { try {
const bodyStr = body as string; const bodyStr = body as string;
// Handle empty body case // Handle empty body case - common for DELETE, GET requests
if (!bodyStr || bodyStr.trim() === '') { if (!bodyStr || bodyStr.trim() === '') {
(req as any).rawBody = bodyStr; (req as any).rawBody = bodyStr;
// For DELETE and GET methods, empty body is expected
if (req.method === 'DELETE' || req.method === 'GET') {
done(null, undefined);
return;
}
// For other methods, return empty object
done(null, {}); done(null, {});
return; return;
} }
@ -111,7 +117,7 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
(req as any).rawBody = bodyStr; (req as any).rawBody = bodyStr;
done(null, json); done(null, json);
} catch (err: any) { } catch (err: any) {
log({ module: 'content-parser', level: 'error' }, `JSON parse error: ${err.message}, body: "${body}"`); log({ module: 'content-parser', level: 'error' }, `JSON parse error on ${req.method} ${req.url}: ${err.message}, body: "${body}"`);
err.statusCode = 400; err.statusCode = 400;
done(err, undefined); done(err, undefined);
} }