← Back to library
Playwrightnetworkintermediatev1.20+

Assert intercepted response body

Capture a real API response in-flight and assert its JSON structure matches the expected contract.

1test('users API response matches expected contract', async ({ page }) => {
2  // Start listening before navigation so we don't miss the request
3  const responsePromise = page.waitForResponse(
4    (res) => res.url().includes('/api/users') && res.status() === 200
5  );
6
7  await page.goto('/users');
8  const response = await responsePromise;
9  const body = await response.json();
10
11  // Assert the shape — catches backend contract breaks before the UI does
12  expect(body).toHaveProperty('users');
13  expect(Array.isArray(body.users)).toBe(true);
14  expect(body.users[0]).toMatchObject({
15    id: expect.any(Number),
16    email: expect.stringContaining('@'),
17  });
18});
#waitForResponse#response body#json#api contract#assert