首页 > 解决方案 > 赛普拉斯:API 请求的身份验证令牌在哪里?

问题描述

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

如何使用赛普拉斯格式化身份验证?

    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

标签: apiauthenticationcypress

解决方案


根据Mailtrap 文档,发送标头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
})

推荐阅读