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:
parent
5ede024b4c
commit
0f222cfd98
@ -99,9 +99,15 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
|
||||
try {
|
||||
const bodyStr = body as string;
|
||||
|
||||
// Handle empty body case
|
||||
// Handle empty body case - common for DELETE, GET requests
|
||||
if (!bodyStr || bodyStr.trim() === '') {
|
||||
(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, {});
|
||||
return;
|
||||
}
|
||||
@ -111,7 +117,7 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
|
||||
(req as any).rawBody = bodyStr;
|
||||
done(null, json);
|
||||
} 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;
|
||||
done(err, undefined);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user