Hello Sir,
I get the Json error as shown in screenshot.
IN the Test tab in the POST res
i have mentioned the below in the script
pm.test(“Status code is 200”, function () {
- pm.expect(pm.response.to.have.status(200));*
});
pm.test(“Response contains color information”, function () {
-
// Generate a random hex color using dynamic variable*
-
var randomHexColor =
#${pm.variables.get("randomNumber").toString(16).padStart(6, "0")}
;* -
// Construct the payload dynamically*
-
var payload = {*
-
“hex”: randomHexColor*
-
};*
-
// Set the request body to the constructed payload*
-
pm.request.body.raw = JSON.stringify(payload);*
-
// Make the request and verify the response structure (assuming HTML)*
-
pm.sendRequest(function (err, response) {*
-
if (err) {*
-
console.error("Error sending request:", err);*
-
return; // Stop execution on error*
-
}*
-
// Check for HTML response (adjust based on API’s response format)*
-
if (response.headers.get(“Content-Type”).includes(“text/html”)) {*
-
// Parse the HTML using cheerio (install it first)*
-
const $ = cheerio.load(response.text());*
-
// Extract color information from HTML (adjust selectors as needed)*
-
const colorName = $("#color-name").text().trim();*
-
const rgbValue = $("#rgb-value").text().trim();*
-
// Construct the payload using extracted information*
-
payload = {*
-
"hex": randomHexColor,*
-
"rgb": rgbValue,*
-
"name": colorName*
-
};*
-
// Set the request body to the constructed payload*
-
pm.request.body.raw = JSON.stringify(payload);*
-
// Send the request to "https://postman-echo.com/post"*
-
pm.sendRequest("https://postman-echo.com/post", function (err, response) {*
-
if (err) {*
-
console.error("Error sending request:", err);*
-
return; // Stop execution on error*
-
}*
-
// Assert successful response and color information*
-
pm.expect(response.status).to.equal(200);*
-
pm.expect(response.json().data.hex).to.equal(randomHexColor);*
-
pm.expect(response.json().data.rgb).to.equal(rgbValue);*
-
pm.expect(response.json().data.name).to.equal(colorName);*
-
});*
-
} else {*
-
// Handle JSON response (if applicable)*
-
pm.expect(response.json()).to.have.property("hex");*
-
// ... other assertions for JSON response*
-
}*
-
});*
});
// Set up a dynamic variable to generate random numbers
pm.variables.set(“randomNumber”, Math.floor(Math.random() * 16777215)); // Generate random number within 0x000000 - 0xFFFFFF range
Can you please help resolve the issue
Thanks,
Kinjal