Send a POST request with JSON body and verify response status
Preconditions (3)
✅ Expected Result: Response status code is 200 and response body contains a token field
pm.test('Status code is 200', function () { pm.response.to.have.status(200); }); pm.test('Response has token', function () { var jsonData = pm.response.json(); pm.expect(jsonData.token).to.exist; pm.expect(jsonData.token).to.be.a('string').that.is.not.empty; });
The first test checks if the response status code is exactly 200, which means the request was successful.
The second test parses the JSON response body and verifies that the token field exists, is a string, and is not empty. This confirms that the login was successful and a token was returned.
Using pm.test allows grouping assertions with clear descriptions. Parsing JSON with pm.response.json() is the standard way to access response data in Postman scripts.
Now add data-driven testing with 3 different sets of user credentials to verify login success or failure.
Send request block in Postman?pm.sendRequest({
url: 'https://api.example.com/data',
method: 'GET'
}, function (err, res) {
pm.test('Status code is 200', function () {
pm.expect(res.code).to.eql(200);
});
});
What will happen when this script runs?pm.sendRequest({
url: 'https://api.example.com/users',
method: 'POST',
body: {
mode: 'raw',
raw: JSON.stringify({name: 'John'})
}
}, function (err, res) {
// callback function
});
But the request is not sent. What is the likely problem?success set to true. Which Postman script correctly sends the request and tests the response?Pass the test and this pattern moves unknown