简体   繁体   中英

Cypress: Where does the authentication token go for an API request?

I am trying to request data through an API that requires authentification. I have the token and I know how to implement it using a manual tool such as postman... 邮递员 API 身份验证

How do I format the authentication using 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'
        }
    })
})

})

Response:

The response we got was:

Status: 401 - Unauthorized

As per the Mailtrap documentation , sending an header Api-Token: {api_token} should send authenticated requests. You can write:

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
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM