12 Mar 2025 09:00 AM
Hello everyone,
I'm encountering an issue with my Synthetic HTTP post-execution script and could use some help troubleshooting.
I have the following script where I define the responseBody directly within the code:
var responseBody = {
"db": [
{
"status": "UP",
"details": {
"db_name": "apig_otk",
"jdbc_name": "OAuth",
"metric_name": "count otk_sessionstore",
"metric_value": 61956,
"threshold": 2000000
}
},
{
"status": "UP",
"details": {
"db_name": "apig_ssg",
"jdbc_name": "SSG",
"metric_name": "count ssg_certificates",
"metric_value": 378,
"threshold": 490
}
},
{
"status": "DOWN",
"details": {
"db_name": "sm_SessionStore",
"jdbc_name": "SM_SessionStore",
"metric_name": "count sm_sessionspec5",
"metric_value": 79,
"threshold": 1000
}
},
{
"status": "UP",
"details": {
"db_name": "sm_SessionStore",
"jdbc_name": "SM_SessionStore",
"metric_name": "count sm_sessionvar5",
"metric_value": 155,
"threshold": 2000
}
}
]
};
With the above, the script works as expected, and I receive a failure message if the status is DOWN.
However, when I modify the script to dynamically retrieve the responseBody using the following lines:
//var responseBody = response.getResponseBody()
//var obj = JSON.parse(responseBody);
And replace the direct reference to responseBody with the parsed object obj, the script doesn't seem to generate any output. No failure message appears, and I can't figure out what's going wrong.
Here's what I'm trying:
var db_array = [];
var metric_name = [];
var metric_value = [];
// First loop to collect data
for (var i = 0; i < obj.db.length; i++) {
var db_var = obj.db[i];
var stato = db_var.status;
var dbName = db_var.details.db_name;
var metricName = db_var.details.metric_name;
var metricValue = db_var.details.metric_value;
// If status is DOWN, push data to arrays
if (stato === "DOWN") {
db_array.push(dbName);
metric_name.push(metricName);
metric_value.push(metricValue);
}
}
// If there are any DOWN dbs, signal the error
if (db_array.length > 0) {
api.fail("The following DBs have status 'DOWN': " + db_array.join() + " with '" + metric_name.join() + "' = " + metric_value.join());
} else {
api.info("All DBs are healthy!");
}
But this doesn't seem to be working. I don't get any failure message, and I’m unable to determine the root cause. Has anyone encountered this issue before, or can anyone point me in the right direction to fix it?
Thank you for any help!
Sofia
Solved! Go to Solution.
14 Apr 2025 01:37 PM
Hello @SofiaPersia
Below is a short script snippet you can copy and paste into your HTTP monitor’s post-execution script. It will help you quickly pinpoint whether there’s an issue with an empty response body, invalid JSON, or a missing db array. Just replace any relevant variable names as needed:
var responseBody = response.getResponseBody();
// 1. Check if the response body is empty
if (!responseBody) {
api.fail("Response body is empty!");
}
try {
// 2. Try parsing the JSON
var obj = JSON.parse(responseBody);
api.info("Successfully parsed JSON.");
} catch (e) {
api.fail("Failed to parse JSON: " + e.message);
}
// 3. Check if the 'db' array exists
if (!obj.db || !Array.isArray(obj.db)) {
api.fail("No 'db' array found in the JSON structure.");
}
// 4. Log basic debug info
api.info("Response seems valid. Proceed with further checks...");
This should help you identify where the script might be failing:
Empty body if nothing is returned.
Parsing errors if the response isn’t valid JSON.
Missing structure if db isn’t present or isn’t an array.
From there, you can add your logic to check status === "DOWN" and report accordingly. Good luck!