How do you check the status, body and headers of a response?
cy.request yields status, body, headers and duration, so every part is checked. A body with Content-Type: application/json is already parsed, header names are lowercase, and content-type is compared with include because its value may carry a charset suffix. Keys and types of the body are checked, and the duration threshold is kept generous. Assertions after the request do not repeat it, so asynchronous states need bounded polling.
Cypress · API testing
The test checks only “status 200”, and the backend quietly changed the price type to a string. What else should the response have been checked for?
An API response has several parts and each breaks in its own way. The status says whether the request was accepted, headers describe the format and caching, the body carries data, and the response time shows whether speed degraded. A test that looks only at the status misses type drift, vanished fields and a wrong Content-Type. This lesson is about what exactly a test gets from cy.request and how to check it.
How it works and why
cy.request returns a response object: status, statusText, body, headers, duration, plus isOkStatusCode and requestHeaders. If the server answered with Content-Type: application/json, the body is already parsed into an object or array; for other types it is a string. Header names in headers are lower-case, so res.headers['content-type'], not Content-Type. The value often carries a suffix such as ; charset=utf-8, so compare with include, not eq.
What lies in a cy.request response
There is an important detail about retries. The cy.request command is not repeated because of assertions or the response status: it is sent once (by default only network failures are retried, retryOnNetworkFailure). Only the following queries on the same value are retried: .its() and .should() re-read a field of the already stored response but do not ask the server again. If data has to “mature” (a queue, background processing), one cy.request cannot wait for it: you need your own polling loop.
Example: from “status 200” to a full check
A weak test checks only the code. A stronger one looks at all four parts:
cy.request('GET', '/api/products/42').then((res) => {
expect(res.status).to.eq(200);
expect(res.headers['content-type']).to.include('application/json');
expect(res.body).to.have.property('id', 42);
expect(res.body.price).to.be.a('number').and.be.greaterThan(0);
expect(res.duration).to.be.lessThan(1000); // ms, a rough threshold
});
// one field: its() extracts, should() asserts
cy.request('/api/products/42').its('body.name').should('eq', 'Flask');
cy.request('/api/products').its('body').should('have.length.greaterThan', 0);
The difference shows when the backend changes: the weak test stays green when price becomes the string "450", while the strong one fails on a('number') with a clear message. Keep the duration threshold rough and generous: time is unstable on CI, and a test with a 200 ms limit will flake. Real speed requirements belong to load-testing tools.
For the response shape, checking keys and types is useful, not only single values:
cy.request('/api/products/42').then(({ body }) => {
// the shape, not only values: keys and types the contract promises
expect(body).to.have.all.keys('id', 'name', 'price', 'tags');
expect(body.tags).to.be.an('array');
expect(body).to.have.property('price').that.is.a('number');
});
all.keys fails both when a field vanished and when an extra one appeared. That is strict, so for responses that grow without harm use include.keys. A full structure check comes from schema validation (a separate lesson).
Limits and alternatives
Several expect calls in .then() stop at the first failure, so you will not see the rest until you fix it. For a JSON response with many fields, deep.include or a schema is handier. In Playwright a response is checked like this: await expect(res).toBeOK(), res.headers() and await res.json(); headers there are lower-case too.
Typical mistakes
- Checking only the status: type, field and format changes slip through.
- Writing
headers['Content-Type']with capitals: the value isundefined. - Comparing
content-typewitheqwhen a; charsetsuffix exists: the test fails for no reason. - A hard
durationlimit: on CI the test becomes flaky. - Expecting
cy.requestto repeat itself until the data appears.
Check yourself
Why does res.headers['Content-Type'] return undefined although the header exists?
Answer
In Cypress response header names are lower-case, so you need res.headers['content-type']. Compare the value with include because it may carry ; charset=utf-8.
Will Cypress repeat cy.request itself if .its('body.status').should('eq', 'done') does not match?
Answer
No. The request is sent once and .its() and .should() only re-read the response already received. To wait for a state change you write your own polling: a recursive function with a new cy.request and a limit on attempts.