← Back to library
Cypressnetworkadvancedv10+

Assert intercepted response body

Intercept a real API response, wait for it, then assert the body structure and values.

1it('users API response matches the expected contract', () => {
2  // Alias the intercept so cy.wait() can reference it by name
3  cy.intercept('GET', '/api/users').as('getUsers');
4
5  cy.visit('/users');
6
7  cy.wait('@getUsers').then(({ response }) => {
8    // Assert on the actual response — catches backend contract breaks early
9    expect(response.statusCode).to.equal(200);
10    expect(response.body).to.have.property('users');
11    expect(response.body.users).to.be.an('array').with.length.greaterThan(0);
12    expect(response.body.users[0]).to.include.keys('id', 'email', 'role');
13  });
14});
#intercept#response body#cy.wait#api contract#assert