Complete the code to specify the HTTP method for retrieving data from a REST API.
pm.sendRequest({ url: 'https://api.example.com/data', method: '[1]' }, function (err, res) { pm.test('Status code is 200', function () { pm.expect(res.code).to.equal(200); }); });The GET method is used to retrieve data from a REST API. Other methods like POST, PUT, and DELETE are used for creating, updating, or deleting data.
Complete the code to check if the response body contains the expected JSON key.
pm.test('Response has userId', function () { var jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('[1]'); });
The test checks if the JSON response contains the key 'userId'. This is common in REST API responses to identify the user.
Fix the error in the assertion to correctly check the response status code.
pm.test('Status code is 201', function () { pm.expect(pm.response.[1]).to.eql(201); });
In Postman tests, the correct property to check the status code is pm.response.code. Using other properties will cause errors or fail the test.
Fill both blanks to create a test that checks if the response time is less than 500 milliseconds.
pm.test('Response time is fast', function () { pm.expect(pm.response.[1]).to.be.[2](500); });
The property pm.response.responseTime gives the response time in milliseconds. The assertion to.be.below(500) checks if it is less than 500 ms.
Fill all three blanks to write a test that verifies the response header 'Content-Type' is 'application/json'.
pm.test('Content-Type is JSON', function () { pm.expect(pm.response.headers.get('[1]')).to.equal('[2]/[3]'); });
The header name is 'Content-Type' (case sensitive in Postman). The expected value is 'application/json' which indicates JSON data format.