Skip to content

Commit

Permalink
fix(filters): do not always parse JSON (#4881)
Browse files Browse the repository at this point in the history
Axios might have already parsed the response as JSON if the response had
the proper headers.
  • Loading branch information
Azenet committed Nov 16, 2021
1 parent c7b4735 commit 33fa30b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/filters/evaluate.ts
Expand Up @@ -32,11 +32,15 @@ const evaluate: ResponseFilter = {
const id = 'url' + crypto.randomBytes(64).toString('hex').slice(0, 5);
const url = match.replace(/url\(['"]|["']\)/g, '');
let response = await axios.get<any>(url);
try {
response.data = JSON.parse(response.data.toString());
} catch (e: any) {
// JSON failed, treat like string
response = response.data.toString();

// do not parse JSON if axios already parsed it
if (typeof(response.data) !== 'object') {
try {
response.data = JSON.parse(response.data.toString());
} catch (e: any) {
// JSON failed, treat like string
response = response.data.toString();
}
}
urls.push({ id, response });
toEvaluate = toEvaluate.replace(match, `url.${id}`);
Expand Down

0 comments on commit 33fa30b

Please sign in to comment.