0
0
CypressHow-ToBeginner ยท 4 min read

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 method to POST, which defaults to GET.
  • Forgetting to include the body or sending it in the wrong format.
  • Not setting the correct Content-Type header, 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

OptionDescriptionExample
methodHTTP method to use'POST'
urlAPI endpoint URL'/api/users'
bodyData sent with the request{ name: 'John' }
headersOptional HTTP headers{ 'Content-Type': 'application/json' }
then()Callback to handle responseresponse => { 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.