我正在为我的 Node 后端 API 编写 Cypress 测试。该测试发送带有一些 header 和正文的 POST 请求,其中包括后端使用的一些必需参数。
奇怪的是端点只接收到一个空的request.body
。
这就是我的 Cypress 测试的样子,您可以看到我如何发送请求正文:
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);
});
});
编辑:这些是我要发送的 header :
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json, text/plain',
'Authorization': 'Bearer <MY_TOKEN>',
};
知道为什么我在后端的 request.body
内没有收到任何内容吗?
最佳答案
我在 Cypress 的 Github 上看到了您的讨论,但您似乎在这里很活跃,所以我会交叉发布。
您的问题是 Content-Type header 。 Cypress 不喜欢 'application/json, text/plain'
,但将其更改为 'application/json'
应该可以修复它。
关于node.js - 为什么我的 Cypress Post 请求的请求正文为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74686557/