【问题标题】:How to handle the plain/text POST request in the cypress如何处理柏树中的纯文本 POST 请求
【发布时间】:2021-12-16 17:02:26
【问题描述】:

我有一个邮递员集合,它是 POST 调用,请求正文是纯/文本类型,我只想使用 cy.request 自动执行此操作,但我不确定如何通过 cy.request 中的测试正文如果我运行以下代码,它会返回 400 错误请求。

 cy.request({
        url: `${url}/user`,
        method: "POST",
   headers: {
            'Content-Type': 'plain/text'
        },
        body: {
            "confirmEmail": "true"
        }
    }).then(res =>{
        cy.task('log',"Email id "+res.body.emailAddress);
        return res.body;
    });
}

上述请求返回 .json 响应,但输入请求如果是文本格式,在邮递员工具中同样可以正常工作。

在邮递员工具中以以下格式传递请求正文并且工作正常。

confirmEmail=true

【问题讨论】:

    标签: automation cypress cypress-component-test-runner


    【解决方案1】:

    我的假设是在请求正文中,我们的端点需要一个布尔值,但您传递的是一个字符串。所以将 "confirmEmail": "true" 更改为 "confirmEmail": true 应该可以工作。

    cy.request({
      url: `${url}/user`,
      method: 'POST',
      headers: {
        'Content-Type': 'plain/text',
      },
      body: {
        confirmEmail: true,
      },
    }).then((res) => {
      cy.log(res.body.emailAddress) //prints email address from response body
    })
    

    如果您需要在 URL 中传递参数,您可以直接使用qs

    cy.request({
      url: `${url}/user`,
      method: 'POST',
      qs: {
        confirmEmail: true,
      },
      headers: {
        'Content-Type': 'plain/text',
      },
    }).then((res) => {
      cy.log(res.body.emailAddress) //prints email address from response body
    })
    

    【讨论】:

    • 不。它仍然返回 400 错误请求。我是否需要在标题部分添加任何参数来解析 cypress 中的文本请求
    • 因此,如果您在 URL 中传递参数,则必须使用 qs。我已经更新了答案,你可以查看。
    猜你喜欢
    • 2019-03-25
    • 1970-01-01
    • 2022-06-13
    • 2013-03-03
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    相关资源
    最近更新 更多