Skip to content

Commit

Permalink
address CR
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Jun 17, 2024
1 parent 3de904e commit 698328d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/helper/REST.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ class REST extends Helper {
}

this.options.prettyPrintJson ? this.debugSection('Request', beautify(JSON.stringify(_debugRequest))) : this.debugSection('Request', JSON.stringify(_debugRequest));
if (this.options.printCurl) this.debugSection('CURL Request', curlize(request));
if (this.options.printCurl) {
if (request.data && !isValidJSON(request.data)) {
this.debugSection('CURL Request', 'cURL is not printed as the request body is not a JSON');
} else {
this.debugSection('CURL Request', curlize(request));
}
}

let response;
try {
Expand Down Expand Up @@ -394,3 +400,17 @@ function curlize(request) {
}
return curl;
}

/**
* Checks if the given text is valid JSON.
* @param {string} text - The text to check.
* @returns {boolean} - Returns true if the text is valid JSON, otherwise false.
*/
function isValidJSON(text) {
try {
JSON.parse(text);
return true;
} catch (error) {
return false;
}
}

0 comments on commit 698328d

Please sign in to comment.