Skip to content

Commit

Permalink
fix: don't try to parse response if request is head #11960
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Aug 29, 2023
1 parent 50d4477 commit 440299e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions public/src/modules/api.js
Expand Up @@ -69,18 +69,23 @@ async function xhr(options, cb) {
const isJSON = contentType && contentType.startsWith('application/json');

let response;
if (isJSON) {
response = await res.json();
} else {
response = await res.text();
if (options.method !== 'head') {
if (isJSON) {
response = await res.json();
} else {
response = await res.text();
}
}

if (!res.ok) {
return cb(new Error(isJSON ? response.status.message : response));
if (response) {
return cb(new Error(isJSON ? response.status.message : response));
}
return cb(new Error(res.statusText));
}

cb(null, (
isJSON && response.hasOwnProperty('status') && response.hasOwnProperty('response') ?
isJSON && response && response.hasOwnProperty('status') && response.hasOwnProperty('response') ?
response.response :
response
));
Expand Down

0 comments on commit 440299e

Please sign in to comment.