Developer Challenge - Day 25 - JSON Error is shown in console

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

Can you please use the pre-formatted text option in the editor when pasting code or JSON. It prevents everything from being aligned to the left in a paragraph which makes it hard to read.

I’m not sure what you are doing with the following section of code.

// 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*
}*

Your sendRequest doesn’t appear to be including the payload, or the URL that you want to send to. Surely this must be causing an error in its own right.

You appear to be setting the payload on the underlying request as that is what “pm.request.body.raw” will do. Not for the sendRequest function.

Somewhere in that code, its expecting a JSON response, but you are getting an HTML response instead (probably an error message).

Probably here.

pm.sendRequest("https://postman-echo.com/post", function (err, response) {

This time you have the URL, but you are not sending a body. The post endpoint for Postman Echo requires a body.

Therefore when you parse the response. Which you do multiple times.

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);

It will probably cause the error.

Get used to console logging your variables. You should parse the response once and store it as a variable, not 3 times, and then use that in the assertions. Plus console log the response, so you know its correct. (Which it doesn’t look like it will be).

Using console logs and break points to step through your code are developer basics. Being that this is part of the developer challenges, I would expect to see a lot of console logs so you can troubleshoot this yourself. You should be able to work out which line is causing the error. You might not know what is causing the error yet, but you should at least know which line is causing the error.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.