From 6bb833fa2262aa2556aaf7af08803dc4043f799d Mon Sep 17 00:00:00 2001 From: Arun K Nair <85106890+AKN414-IND@users.noreply.github.com> Date: Mon, 30 Mar 2026 22:25:59 +0530 Subject: [PATCH] fix: prevent JSON parsing when responseType is text --- src/index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index cca6534..aead3c8 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,7 @@ function create(defaults) { * @param {(...args: Args[]) => R} fn * @returns {(array: Args[]) => R} */ - redaxios.spread = (fn) => /** @type {any} */ (fn.apply.bind(fn, fn)); + redaxios.spread = (fn) => /** @type {any} */(fn.apply.bind(fn, fn)); /** * @private @@ -181,7 +181,7 @@ function create(defaults) { // @ts-ignore accessing match()[2] throws for no match, which is intentional document.cookie.match(RegExp('(^|; )' + options.xsrfCookieName + '=([^;]*)'))[2] ); - } catch (e) {} + } catch (e) { } if (options.baseURL) { url = url.replace(/^(?!.*\/\/)\/?/, options.baseURL + '/'); @@ -213,8 +213,16 @@ function create(defaults) { return res[options.responseType || 'text']() .then((data) => { response.data = data; - // its okay if this fails: response.data will be the unparsed value: - response.data = JSON.parse(data); + + // Only attempt to parse if the user actually wants JSON + // or if using the default 'json' type + if (!options.responseType || options.responseType === 'json') { + try { + response.data = JSON.parse(data); + } catch (e) { + // do nothing and retur the no parsed data + } + } }) .catch(Object) .then(() => {