← Back to library
Cypressnetworkintermediatev10+
Intercept & stub a POST request
Intercept a POST request and return a stubbed response — no real server needed.
1describe('Form submission', () => {
2 it('shows success message after submit', () => {
3 cy.intercept('POST', '/api/contact', {
4 statusCode: 200,
5 body: { message: 'Sent!' },
6 }).as('submitForm');
7
8 cy.visit('/contact');
9 cy.get('[name="email"]').type('test@test.com');
10 cy.get('[name="message"]').type('Hello');
11 cy.get('[type="submit"]').click();
12
13 cy.wait('@submitForm');
14 cy.contains('Sent!').should('be.visible');
15 });
16});#intercept#stub#mock#api