在 Cypress JS 中使用 --data-raw 发布请求

发布于 2025-01-14 19:44:06 字数 915 浏览 1 评论 0原文

再会!

我在 JS cypress 上的自动测试有问题。 我需要在自动测试中发送发布请求。这是我用手发送的:

curl --location --request POST 'http://test.com/service' \
--header 'Token: n7n7n7n7n7' \
--header 'Content-Type: text/plain' \
--data-raw 'text: v3'

这是我尝试在 js 测试中发送帖子的方式:

cy.request({
            method: 'POST',
            url: 'http//:test.com/service',
            headers: {
                'Token': n7n7n7n7n7,
                'Content-Type':'text/plain'
            },
            data: "text: v3"
        }).then((res)=>{
            expect(res.status).to.eq(200)
        })

它不起作用,而不是 200,我得到 400,在 cypress 日志中我看到:

我们发送的请求是:

方法:POST 网址:http://:test.com/service 标题:{ “连接”:“保持活动”, "X-Gitlab-Token": "n7n7n7n7n7", “内容类型”:“文本/纯文本”, “用户代理”:“Mozilla/5.0”, “接受”: ”/”, "accept-encoding": "gzip, deflate", “内容长度”:0 并且

没有数据。

如何在 cypress 请求中发送原始数据? 提前致谢!

Good day!

I have a problem with my autotest on JS cypress.
I need to send post request in autotest. Here it is as I send it by hands:

curl --location --request POST 'http://test.com/service' \
--header 'Token: n7n7n7n7n7' \
--header 'Content-Type: text/plain' \
--data-raw 'text: v3'

This is how I try to send post in js test:

cy.request({
            method: 'POST',
            url: 'http//:test.com/service',
            headers: {
                'Token': n7n7n7n7n7,
                'Content-Type':'text/plain'
            },
            data: "text: v3"
        }).then((res)=>{
            expect(res.status).to.eq(200)
        })

It doesnt work, instead of 200 I get 400, in cypress log I see:

The request we sent was:

Method: POST
URL: http//:test.com/service
Headers: {
"Connection": "keep-alive",
"X-Gitlab-Token": "n7n7n7n7n7",
"Content-Type": "text/plain",
"user-agent": "Mozilla/5.0",
"accept": "/",
"accept-encoding": "gzip, deflate",
"content-length": 0
}

And no data.

How can I send data-raw in cypress request?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2025-01-21 19:44:06

让我们尝试一下脚本:
cy.请求({
方法:'POST',
url: 'http://:test.com/service',
标题:{
'授权': "n7n7n7n7n7",
'内容类型':'文本/纯文本'
},
正文:{“文本”:“v3”}
}).then((res)=>{
期望(res.status).to.eq(200)
})

Let's try with the script:
cy.request({
method: 'POST',
url: 'http//:test.com/service',
headers: {
'authhorization': "n7n7n7n7n7",
'Content-Type':'text/plain'
},
body: {"text": "v3"}
}).then((res)=>{
expect(res.status).to.eq(200)
})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文