Test Overview
This test sends a POST request with a JSON body to an API endpoint. It verifies that the API accepts the body format and returns a success status code.
This test sends a POST request with a JSON body to an API endpoint. It verifies that the API accepts the body format and returns a success status code.
pm.test("Body format matches API expectations", function () { const requestBody = { "username": "testuser", "password": "securePass123" }; pm.sendRequest({ url: pm.environment.get("apiUrl") + "/login", method: 'POST', header: { 'Content-Type': 'application/json' }, body: { mode: 'raw', raw: JSON.stringify(requestBody) } }, function (err, res) { pm.expect(err).to.be.null; pm.expect(res).to.have.property('status', 200); pm.expect(res.json()).to.have.property('token').that.is.a('string'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman test runner is ready | — | PASS |
| 2 | Prepare JSON body with username and password | Request body is set as JSON string | — | PASS |
| 3 | Send POST request to API endpoint /login with JSON body and Content-Type header | Request sent to server | — | PASS |
| 4 | Receive response from API | Response received with status code and JSON body | Check no error in request | PASS |
| 5 | Assert response status code is 200 | Response status code is 200 | Status code equals 200 | PASS |
| 6 | Assert response JSON contains a token string | Response JSON has token property | Token property exists and is a string | PASS |
| 7 | Test ends with all assertions passed | Test report shows success | — | PASS |
Content-Type header with the body format in an API request?Content-Type header tells the API what format the request body is in, like JSON or XML.{
"name": "Alice",
"age": 30
}Content-Type: application/json, what will the API most likely do if it expects JSON?name=Bob&age=25
Content-Type: application/json. The API returns a 400 Bad Request error. What is the likely cause?{
"user": {
"name": "Eve",
"details": {
"age": 28,
"city": "NY"
}
}
}Pass the test and this pattern moves unknown