Fix empty-body JSON error on body-less admin requests
The shared cloud req() helper set content-type: application/json on every request, so body-less DELETE/revoke/rotate calls tripped Fastify's "Body cannot be empty" 400. Only send the header when there's a body, and (defensively) make the server parse an empty application/json body as undefined instead of erroring. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,11 @@ export interface CloudCharInfo { id: string; name: string; ownerUserId: string;
|
||||
export async function req<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetch(`${base()}${path}`, { ...init, headers: { 'content-type': 'application/json', ...authHeaders(), ...(init?.headers ?? {}) } });
|
||||
// Only declare a JSON content-type when we actually send a body — Fastify
|
||||
// rejects an empty body that claims content-type: application/json, which
|
||||
// broke the body-less DELETE/revoke/rotate calls.
|
||||
const jsonType = init?.body != null ? { 'content-type': 'application/json' } : {};
|
||||
res = await fetch(`${base()}${path}`, { ...init, headers: { ...jsonType, ...authHeaders(), ...(init?.headers ?? {}) } });
|
||||
} catch {
|
||||
throw new CloudError('Could not reach the server.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user