Fix double read of json response when content-length header is missing

This commit is contained in:
2025-10-04 22:16:28 +02:00
parent 7272212081
commit 8ebdb1a183
3 changed files with 11 additions and 8 deletions

View File

@@ -49,11 +49,8 @@ async function apiCall<T>(
} else {
// Handle empty responses for JSON endpoints
const contentLength = response.headers.get('content-length');
if (contentLength === '0' || contentLength === null) {
const text = await response.text();
if (text.trim() === '') {
return {} as T; // Return empty object for empty JSON responses
}
if (contentLength === '0') {
return {} as T; // Return empty object for empty JSON responses
}
const data = await response.json() as T;
return data;

View File

@@ -26,7 +26,8 @@ export async function handleApiError(response: Response): Promise<void> {
}
if (!response.ok) {
const errorMessage = await parseErrorResponse(response)
// Clone the response before reading to avoid consuming the body stream
const errorMessage = await parseErrorResponse(response.clone())
throw new Error(errorMessage)
}
}