I requested a POST method in API call for login test and received array with many objects in the body. I wanna access a specific client token using its method, but it is inside a array and I cannot quite figure out how to access, because the array doesn't have a "name".
The request in cypress:
it('Logar em um cliente com um usuário', function () {
cy.request({
method: 'POST',
url: 'https://localhost:44332/api/Users/LoginDefault',
body: {
"username": "user",
"password": "password"
}
}).its('body.token').then(res => console.log(res))
The body response (resumed):
[
{
"user": "user1",
"token": "token1"
},
{
"user": "user2",
"token": "token2"
},
{
"user": "user3",
"token": "token3"
}
]
SOLUTION It worked like this:
it('Logar em um cliente com um usuário', function () {
cy.request({
method: 'POST',
url: 'https://localhost:44332/api/Users/LoginDefault',
body: {
"username": "user",
"password": "password"
}
}).its('body').then((res) => {
const dadoToken = res[1].token
expect(dadoToken).not.to.be.empty
})
})
In this situation that body response is an array, so all you need to do is navigate the array.
The return value is res
, so in this situation, it would be res[i].token
where i
is the object in the array that you require.
For example res[2].token
would be "token3".