【问题标题】:Cypress: Where does the authentication token go for an API request?赛普拉斯:API 请求的身份验证令牌在哪里?
【发布时间】:2020-12-07 20:43:29
【问题描述】:

我正在尝试通过需要身份验证的 API 请求数据。我有令牌,我知道如何使用邮递员等手动工具来实现它......

如何使用 Cypress 格式化身份验证?

    it('GET - read', () => {
    cy.request({
        method: 'GET',
        url: 'https://mailtrap.io/api/v1/inboxes/123/messages?page=1&last_id=&useremail',
        headers: {
            Key: 'Api-Token',
            Value: '1234'
        }
    })
})

})

回复:

The response we got was:

Status: 401 - Unauthorized

【问题讨论】:

  • 你能不能把key换成Key再试一次。
  • 感谢您的评论。试过了,但不幸的是,我得到了同样的回应

标签: api authentication cypress


【解决方案1】:

根据Mailtrap documentation,发送标头Api-Token: {api_token} 应该发送经过身份验证的请求。你可以写:

cy.request({
    method: 'GET',
    url: 'https://mailtrap.io/api/v1/inboxes/123/messages?page=1&last_id=&useremail',
    headers: {
        'Api-Token': 'Your Token value' //Replace with real token
    },
    failOnStatusCode: false
}).then((res) => {
    expect(res.status).to.equal(200) //Replace with releveant 2xx response code
})

【讨论】:

  • 谢谢。我仍然收到 401 响应 - 'body: {error: "Your api token or JWT is invalid"}'。我知道令牌是有效的,因为它通过邮递员工作。所以我认为它希望在哪里以及如何格式化令牌存在问题?
  • 我已经更新了我的答案。你能检查一下吗?
猜你喜欢
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多