Unable to get object value from response body in cypress

129 Views Asked by At

I am a beginner to Cypress and I am trying to get hands-on in API Testing for Cypress. I am trying a very basic GET response from the following URL: https://automationexercise.com/api/productsList

I am trying to get responseCode, but whenever I try to object, I get blank value on it.. my logic is following:

it("Verify All Product response contains correct keys and values", () => {

    cy.request("GET","https://automationexercise.com/api/productsList").then((response) => {
        expect(response.status).to.eq(200)
        expect(response.body).length.to.be.greaterThan(1)
        cy.log(response.body.responseCode)
  });
});

I have edited with screenshot of my result: enter image description here Let me know, where I am making a mistake. Thanks.

1

There are 1 best solutions below

4
On

Don't use cy.log() for debugging, use console.log().

If you open up a browser (not in Cypress), open the devtools, go to Network and turn on recording.

Then put your URL into the navigation box - the devtools shows you the network request and you can inspect the response in the Response sub-tab.

From that, you can see there is no body in the path, so don't add it in your test.

enter image description here