【问题标题】:Post request with --data-raw in Cypress JS在 Cypress JS 中使用 --data-raw 发布请求
【发布时间】:2023-01-10 10:21:15
【问题描述】:

再会!

我在 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 日志中我看到:

我们发送的请求是:

方法:邮寄 网址:http//:test.com/service 标题:{ “连接”:“保持活动状态”, "X-Gitlab-令牌": "n7n7n7n7n7", “内容类型”:“文本/纯文本”, "用户代理": "Mozilla/5.0", “接受”: ”/”, “接受编码”:“gzip,放气”, “内容长度”:0 }

而且没有数据。

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

【问题讨论】:

  • 你能检查你的网址是否正确吗?目前看到它是http:,但我希望是http://
  • 抱歉,我更改了此网址以在此处发帖,这只是一个错字
  • 400 响应中是否有关于正在发生的事情的任何信息?
  • 我找到了方法:因此,如果您需要发送原始数据 - 请在此处请求示例:``` cy.request({ method: 'POST', url: 'http//:test.com/service', headers: { 'Token': n7n7n7n7n7, 'Content-Type': 'text/plain' }, body: "text: v3" }).then((res)=>{ expect(res.status).to.eq(200) })```

标签: javascript post http-post cypress


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多