You can write something like:
cy.request({
body: {},
failOnStatusCode: false,
}).then((res) => {
if (res.status == 404) {
cy.log(JSON.stringify(res.body)) //prints the response body
cy.log(res.body.name) //prints name
cy.log(res.body.message) //prints message
} else {
//Do Something else
}
})
You can use the fail event to log your response if any criteria fails.
cy.request({
...
})
.then(res => {
Cypress.once('fail', (error, runnable) => {
cy.log(JSON.stringify(res))
throw error // throw error to have test still fail
})
expect(...
expect(...
expect(...
})