From 794389a7c20ccf058bdeedca2a76473cef8f6cad Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 19 Mar 2024 12:04:43 +0100 Subject: [PATCH] Bug 36351: Fix http-client when response is not JSON Signed-off-by: Nick Clemens --- .../intranet-tmpl/lib/koha/cateditor/koha-backend.js | 2 +- koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js index 2fded33d038..8586206ade0 100644 --- a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js +++ b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js @@ -139,7 +139,7 @@ define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=defin const client = APIClient.cataloguing; client.catalog_bib.create({ frameworkcode, record }).then( success => { - var record = _fromXMLStruct( data ); + var record = _fromXMLStruct( success ); if ( record.marcxml ) { record.marcxml[0].frameworkcode = frameworkcode; } diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js index f5340df9467..a379a1f8675 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js @@ -38,10 +38,11 @@ class HttpClient { headers: { ...this._headers, ...headers }, }) .then(response => { + const is_json = response.headers.get("content-type")?.includes("application/json"); if (!response.ok) { return response.text().then(text => { let message; - if (text) { + if (text && is_json) { let json = JSON.parse(text); message = json.error || @@ -53,7 +54,10 @@ class HttpClient { throw new Error(message); }); } - return return_response ? response : response.json(); + if ( return_response || !is_json ) { + return response; + } + return response.json(); }) .then(result => { res = result; -- 2.30.2