Complete the code to send a GET request to the API endpoint.
cy.request('[1]').then((response) => { expect(response.status).to.eq(200) })
The cy.request command requires the URL path as a string. The correct endpoint is '/api/users'.
Complete the code to set up a POST request with JSON body in the API-first test.
cy.request({
method: 'POST',
url: '/api/users',
[1]: { name: 'Alice' }
}).then((response) => {
expect(response.status).to.eq(201)
})data or payload instead of body.The cy.request command uses the body property to send JSON data in the request.
Fix the error in the assertion to check the response body contains the user name.
cy.request('/api/users/1').then((response) => { expect(response.body.[1]).to.eq('Alice') })
username or userName.The API response body contains the property name for the user's name, so the assertion should check response.body.name.
Fill both blanks to create a setup function that creates a user via API and stores the user ID.
function setupUser() {
return cy.request({
method: 'POST',
url: '/api/users',
[1]: { name: 'Bob' }
}).then((response) => {
const userId = response.body.[2];
return userId;
});
}payload instead of body.userId in the response.The request data is sent in the body property, and the API returns the new user's ID in response.body.id.
Fill all three blanks to write a test that sets up a user, then verifies the user exists via GET request.
it('creates and verifies a user', () => { setupUser().then(([1]) => { cy.request('/api/users/' + [2]).then((response) => { expect(response.body.[3]).to.eq('Bob') }) }) })
The setupUser function returns the userId, which is used to request the user data. The response body contains the name property to verify.