Skip to content

Cypress - Get and Post

Cypress - API Testing with GET and POST Requests

Section titled “Cypress - API Testing with GET and POST Requests”

Cypress is not just for UI testing; it also excels at API testing. You can use the cy.request() command to send HTTP requests directly to your server endpoints, allowing you to test your API’s functionality, performance, and reliability independently of the UI.

The HTTP GET method is used to retrieve data from a specified resource. When using cy.request() for a GET operation, you typically provide the method (‘GET’) and the URL. Cypress then makes the request and allows you to assert on the response.

A successful GET request usually returns a status code of 200 (OK). You should also verify other aspects of the response, such as headers and the body content. If you’ve set a baseUrl in your cypress.config.js, cy.request() will automatically use it for relative URLs.

Here’s an example of making a GET request and validating its response:

describe('API GET Request Test', () => {
it('Should retrieve comments and validate the response', () => {
// Assuming baseUrl might be 'https://jsonplaceholder.cypress.io' in cypress.config.js
// If not, use the full URL: 'https://jsonplaceholder.cypress.io/comments'
cy.request('GET', 'https://jsonplaceholder.cypress.io/comments')
.then((response) => {
expect(response.status).to.eq(200);
expect(response.body).to.be.an('array').and.not.be.empty;
expect(response).to.have.property('headers');
expect(response).to.have.property('duration'); // Cypress adds this
// Example: Check a specific comment's properties if needed
// expect(response.body[0]).to.have.property('email');
});
});
});

Execution Results:

Upon execution, Cypress will log the GET request and its details in the Command Log. If all assertions pass, the test will be marked as successful, confirming that the API endpoint correctly returned the expected data and status.

The HTTP POST method is used to send data to a server to create or update a resource. When using cy.request() for a POST operation, you’ll typically include the method (‘POST’), the URL, and a request body containing the data to be sent.

A successful POST request that creates a new resource often returns a status code of 201 (Created). The response body might contain the newly created resource or a confirmation message.

Below is an example of making a POST request to create a new resource:

describe('API POST Request Test', () => {
it('Should create a new post and validate the response', () => {
const newPost = {
title: 'My Cypress Test Post',
body: 'This post was created by a Cypress API test.',
userId: 1,
};
cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', newPost)
.then((response) => {
expect(response.status).to.eq(201); // 201 Created
expect(response.body).to.have.property('title', newPost.title);
expect(response.body).to.have.property('body', newPost.body);
expect(response.body).to.have.property('userId', newPost.userId);
expect(response.body).to.have.property('id'); // Usually a server-generated ID
});
});
});

Execution Results:

The Cypress Command Log will show the POST request, including its payload and the server’s response. A successful test indicates that the API correctly processed the POST request and created the resource as expected.

cy.request() supports all standard HTTP methods, including PUT, DELETE, PATCH, HEAD, OPTIONS, etc. You can also configure headers, authentication, query parameters, and more. For detailed options, refer to the official Cypress documentation for cy.request(). This command is essential for comprehensive API testing strategies, such as setting up application state or verifying backend logic.