Hello, Im stuck on day 26 and not even chat GPT can help me.
Any sugestions? the “submit” request is still showing the error "JSONError: No data, empty input at 1:1 "
I tried removing this section of the test and clearly the problem is with these validations:
let collVar = collection.variable.find(variable => { return variable.key === 'links'})
pm.expect(collVar.key, 'check collection variable set').to.equal('links')
pm.expect(JSON.parse(collVar.value), 'check collection variable array').to.be.an('array')
My request “bing” is returning 200 and working fine,
this is my code:
var cheerio = require('cheerio');
var htmlResponse = pm.response.text();
console.log("HTML Response:", htmlResponse);
const $ = cheerio.load(htmlResponse);
let links = [ ];
$('a').each(function () {
let href = $(this).attr('href');
if (href && href.startsWith("http")) {
links.push(href);
}
});
pm.collectionVariables.set('links', JSON.stringify(links));
console.log("Links variable set:", JSON.stringify(links));
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Validate the array", function () {
var linksJsonString = pm.collectionVariables.get("links");
console.log("Links JSON string:", linksJsonString);
if (linksJsonString) {
try {
var storedLinks = JSON.parse(linksJsonString);
console.log("Stored Links:", storedLinks);
console.log("Extracted Links:", links);
pm.expect(links).to.eql(storedLinks);
} catch (error) {
console.error("Error parsing JSON:", error);
}
} else {
console.error("The 'links' variable is empty or undefined.");
}
});