small fix in api

This commit is contained in:
Mateusz Gruszczyński
2026-03-04 10:33:48 +01:00
parent 721ad44960
commit 013a73e02d
3 changed files with 25 additions and 34 deletions

View File

@@ -42,15 +42,16 @@ function cacheHeaderSummary(headers) {
async function readBodyAuto(response) {
const ct = (response.headers.get('content-type') || '').toLowerCase();
const raw = await response.text();
const isJson = ct.includes('application/json');
if (isJson) {
try {
return { kind: 'json', data: await response.json(), contentType: ct };
return { kind: 'json', data: JSON.parse(raw), contentType: ct, raw };
} catch {
return { kind: 'text', data: await response.text(), contentType: ct };
return { kind: 'text', data: raw, contentType: ct, raw };
}
}
return { kind: 'text', data: await response.text(), contentType: ct };
return { kind: 'text', data: raw, contentType: ct, raw };
}
function safeParseJsonFromTextarea(textareaId) {
@@ -255,7 +256,7 @@ function downloadFromApiJsonTextarea(endpoint, textareaId, fileBaseName) {
const url = baseUrl + '/api/' + endpoint;
fetchAsBlob(url, 'POST', body)
.then(async ({ blob, headersText, cacheSummary, contentType, contentDisposition }) => {
.then(async ({ blob, headersText, cacheSummary, contentType }) => {
const ext = guessExtensionFromContentType(contentType);
const filename = `${fileBaseName}.${ext}`;
@@ -278,7 +279,6 @@ function downloadFromApiJsonTextarea(endpoint, textareaId, fileBaseName) {
});
}
function previewTextFromGenerate(textareaId) {
const { body: out } = showResponse('response-generate-download');
@@ -291,7 +291,7 @@ function previewTextFromGenerate(textareaId) {
}
fetchAsBlob(baseUrl + '/api/generate', 'POST', body)
.then(async ({ blob, headersText, cacheSummary, contentType }) => {
.then(async ({ blob, headersText, cacheSummary }) => {
let text = '';
try { text = await blob.text(); } catch { text = '(binary content)'; }