How to Make GET Request in Cypress: Simple Guide
In Cypress, you make a GET request using
cy.request() with the method set to GET. This command sends the request and lets you assert the response easily within your test.Syntax
The basic syntax for a GET request in Cypress uses cy.request() with an options object. You specify the method as GET and provide the url to request.
- method: HTTP method, here it is
GET. - url: The endpoint you want to call.
- headers (optional): Any headers you want to send.
- qs (optional): Query string parameters as an object.
javascript
cy.request({
method: 'GET',
url: '/api/users',
headers: { 'Authorization': 'Bearer token' },
qs: { page: 1, limit: 10 }
}).then((response) => {
// assertions here
})Example
This example shows how to make a GET request to fetch users from an API and check that the response status is 200 and the body contains a list.
javascript
describe('GET Request Example', () => { it('fetches users and checks response', () => { cy.request({ method: 'GET', url: 'https://jsonplaceholder.typicode.com/users' }).then((response) => { expect(response.status).to.equal(200) expect(response.body).to.be.an('array') expect(response.body.length).to.be.greaterThan(0) }) }) })
Output
Test passes if status is 200 and response body is a non-empty array.
Common Pitfalls
Common mistakes when making GET requests in Cypress include:
- Not specifying the
methodexplicitly (though GET is default, being explicit is clearer). - Using incorrect URL or missing base URL configuration.
- Not handling asynchronous
cy.request()properly with.then(). - Trying to assert on response before the request completes.
Always chain assertions inside the .then() callback to ensure the request finished.
javascript
/* Wrong way: assertions outside then, will fail */ cy.request('/api/users') // expect(true).to.be.true // This runs before request finishes /* Right way: assertions inside then */ cy.request('/api/users').then((response) => { expect(response.status).to.equal(200) })
Quick Reference
| Option | Description | Example |
|---|---|---|
| method | HTTP method to use | 'GET' |
| url | API endpoint URL | '/api/users' |
| headers | Headers object | { 'Authorization': 'Bearer token' } |
| qs | Query string parameters | { page: 1, limit: 10 } |
| timeout | Request timeout in ms | 5000 |
Key Takeaways
Use cy.request() with method 'GET' to make GET requests in Cypress.
Always put assertions inside the .then() callback to wait for the response.
Specify URL and optional headers or query parameters clearly.
Check response status and body to validate API behavior.
Avoid making assertions outside the asynchronous request callback.