How to Make POST Request in Cypress: Syntax and Example
In Cypress, you make a POST request using
cy.request() with the method set to POST and include the request body as an object. This lets you test API endpoints by sending data and verifying responses easily.Syntax
The basic syntax for a POST request in Cypress uses cy.request() with an options object. You specify the method as POST, the url to send the request to, and the body containing the data you want to send.
- method: HTTP method, here it is
POST. - url: The API endpoint URL.
- body: The data sent with the request, usually an object.
- headers: Optional, to set content type or authorization.
javascript
cy.request({
method: 'POST',
url: '/api/endpoint',
body: {
key1: 'value1',
key2: 'value2'
},
headers: {
'Content-Type': 'application/json'
}
}).then((response) => {
// assertions here
})Example
This example shows how to send a POST request to create a new user and check that the response status is 201 (Created) and the response body contains the expected user data.
javascript
describe('POST request example', () => { it('creates a new user successfully', () => { cy.request({ method: 'POST', url: 'https://jsonplaceholder.typicode.com/users', body: { name: 'John Doe', email: 'john@example.com' }, headers: { 'Content-Type': 'application/json' } }).then((response) => { expect(response.status).to.equal(201); expect(response.body).to.have.property('name', 'John Doe'); expect(response.body).to.have.property('email', 'john@example.com'); }); }); });
Output
Test passes if response status is 201 and response body contains the sent user data.
Common Pitfalls
Common mistakes when making POST requests in Cypress include:
- Not setting the
methodtoPOST, which defaults toGET. - Forgetting to include the
bodyor sending it in the wrong format. - Not setting the correct
Content-Typeheader, causing the server to reject the request. - Not handling asynchronous
then()properly, leading to flaky tests.
Always verify the API expects JSON or another format and set headers accordingly.
javascript
/* Wrong way: missing method and body */ cy.request('/api/users').then((response) => { // This sends a GET request, not POST }); /* Right way: specify method and body */ cy.request({ method: 'POST', url: '/api/users', body: { name: 'Alice' }, headers: { 'Content-Type': 'application/json' } }).then((response) => { expect(response.status).to.equal(201); });
Quick Reference
| Option | Description | Example |
|---|---|---|
| method | HTTP method to use | 'POST' |
| url | API endpoint URL | '/api/users' |
| body | Data sent with the request | { name: 'John' } |
| headers | Optional HTTP headers | { 'Content-Type': 'application/json' } |
| then() | Callback to handle response | response => { expect(response.status).to.equal(201) } |
Key Takeaways
Use cy.request() with method: 'POST' and include the body object to send data.
Always set the correct Content-Type header, usually 'application/json', for POST requests.
Check the response status and body inside the then() callback to verify success.
Avoid omitting the method or body to prevent sending unintended GET requests.
Use assertions inside then() to make your tests reliable and clear.