Xar
Xar

Reputation: 7940

Why is the request body of my Cypress Post request empty?

I am writing a Cypress test for my Node backend API. The test sends a POST request with some headers and body, which includes some required parameters for the backend to work with.

The strange thing is that the endpoint is just receiving an empty request.body.

This is how my Cypress test looks like, you can see how I send the request body:

    it('onboards a repo - POST', () => {
        cy.request({
            method: 'POST',
            url: `/my/${org}/endpoint`,
            headers: headers,
            body: {
                productId: productId,
                requestorEmail: requestorEmail
            }
        }).then( (response) => {
            expect(response.status).to.eq(200);
        });
    });

Edit: These are the headers I'm sending:

    const headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json, text/plain',
        'Authorization': 'Bearer <MY_TOKEN>',
    };

Any idea why I am not receiving anything inside the request.body in the backend?

Upvotes: 2

Views: 1284

Answers (1)

DJSDev
DJSDev

Reputation: 1003

I saw your discussion on Cypress's Github, but you seem to be active on here, so I'll crosspost.

Your issue is the Content-Type header. Cypress doesn't like 'application/json, text/plain', but changing it to 'application/json' should fix it.

Upvotes: 3

Related Questions