首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Cypress发送POST时来自API的错误消息

使用Cypress发送POST时来自API的错误消息
EN

Stack Overflow用户
提问于 2021-07-13 15:43:57
回答 1查看 118关注 0票数 0

我尝试用cy.request发送API请求,但数据格式似乎有问题。我的代码如下所示:

代码语言:javascript
复制
cy.request({
                    url: Cypress.env("RtmApiUrl") + "/test-plan/",
                    method: "POST",
                    headers: {
                        "Authorization": "Bearer " + Cypress.env('RtmApiToken'),
                        "content-type": "application/json",
                        body: {
                            "projectKey": "QAIR",
                            "summary": "API Test Regression",
                            "description": "Full regression",
                            "parentTestKey": "F-QAIR-TP-8",
                            "priority": {
                                "id": 3,
                                "name": "Medium"
                            },
                            "status": {
                                "id": 10005,
                                "name": "Backlog"
                            },
                            "includedTestCases": []
                        },
                    },
                });

以及Cypress Test Runner的控制台输出:

代码语言:javascript
复制
    CypressError: `cy.request()` failed on:

https://rtm-api.hexygen.com/api/test-plan/

The response we received from your web server was:

  > 400: Bad Request

This was considered a failure because the status code was not `2xx` or `3xx`.

If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`

-----------------------------------------------------------

The request we sent was:

Method: POST
URL: https://rtm-api.hexygen.com/api/test-plan/
Headers: {
  "Connection": "keep-alive",
  "Authorization": "Bearer <some Token>",
  "content-type": "application/json",
  "body": {
    "projectKey": "QAIR",
    "summary": "API Test Regression",
    "description": "Full regression",
    "parentTestKey": "F-QAIR-TP-8",
    "priority": {
      "id": 3,
      "name": "Medium"
    },
    "status": {
      "id": 10005,
      "name": "Backlog"
    },
    "includedTestCases": []
  },
  "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
  "accept": "*/*",
  "accept-encoding": "gzip, deflate",
  "content-length": 0
}

-----------------------------------------------------------

The response we got was:

Status: 400 - Bad Request
Headers: {
  "server": "Cowboy",
  "connection": "keep-alive",
  "x-content-type-options": "nosniff",
  "x-xss-protection": "1; mode=block",
  "cache-control": "no-cache, no-store, max-age=0, must-revalidate",
  "pragma": "no-cache",
  "expires": "0",
  "strict-transport-security": "max-age=31536000 ; includeSubDomains",
  "x-frame-options": "DENY",
  "content-type": "application/json",
  "content-length": "69",
  "date": "Tue, 13 Jul 2021 07:32:36 GMT",
  "via": "1.1 vegur"
}
Body: {
  "errorMessages": [
    "No content to map to Object due to end of input"
  ]
}

我已经用Postman和curl尝试了相同的header和body组合,它起作用了。我是不是漏掉了什么?

任何帮助都是非常感谢的。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-13 16:59:58

各位,我遗漏了headers代码块末尾的一个括号。所以这是一个非常经典的layer 8问题…

代码语言:javascript
复制
    cy.request({
                    url: Cypress.env("RtmApiUrl") + "/test-plan/",
                    method: "POST",
                    headers: {
                        "Authorization": "Bearer " + Cypress.env('RtmApiToken'),
                        "content-type": "application/json"
                    },
                    body: {
                        "projectKey": "QAIR",
                        "summary": "API Test Regression",
                        "description": "Full regression",
                        "parentTestKey": "F-QAIR-TP-8",
                        "priority": {
                            "id": 3,
                            "name": "Medium"
                        },
                        "status": {
                            "id": 10005,
                            "name": "Backlog"
                        },
                        "includedTestCases": []
                    },
                },
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68358405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档