API Testing Components
Key Components of an API Request/Response
- Endpoint (URL): the address the request is sent to.
- HTTP Method: GET, POST, PUT, DELETE, PATCH — indicating the action to perform.
- Headers: metadata such as content type or authorization tokens.
- Request Body/Payload: the data sent with the request (commonly JSON).
- Status Code: a numeric response indicating success or failure (e.g., 200, 404, 500).
- Response Body: the data returned by the server.
Why Understanding These Matters
A tester needs to know exactly which of these components to construct for the request and which to validate in the response to write an effective API test.
Anatomy of a Real Request and Response
Here is a complete example showing every component together. The client creates an order:
POST /api/v1/orders?notify=true HTTP/1.1
Host: shop.example.com
Content-Type: application/json
Authorization: Bearer <token>
{
"productId": 4521,
"quantity": 2
}
The server replies:
HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/v1/orders/9876
{
"orderId": 9876,
"status": "PLACED",
"total": 1598.00
}
Notice two pieces not listed earlier: query parameters (notify=true) that modify the request through the URL, and path parameters such as the order ID inside /orders/9876. Response headers like Location also carry useful information worth testing.
A good habit is to follow the Location header with a GET request. If the new order comes back with the same product and quantity you sent, you have confirmed that the data was really saved, not just acknowledged.
What to Check in Each Component
| Component | What a Tester Verifies |
|---|---|
| Endpoint and parameters | Correct path, required parameters enforced, invalid IDs handled |
| Method | Unsupported methods return 405; GET does not change data |
| Headers | Missing or wrong Content-Type rejected; Authorization required where expected |
| Body | Required fields, data types, length limits, extra or missing fields |
| Status code | Matches the outcome: 201 for create, 400 for bad input, 401 or 403 for auth problems, 404 for missing resources |
| Response body | Field values, data types, structure against the agreed schema, helpful error messages |
| Response time | Stays within the agreed limit for typical requests |
Status Code Families at a Glance
- 2xx success: 200 OK, 201 Created, 204 No Content.
- 3xx redirection: the resource lives elsewhere.
- 4xx client errors: 400 Bad Request, 401 Unauthorized (not logged in), 403 Forbidden (logged in but not allowed), 404 Not Found, 409 Conflict, 422 validation failure in many APIs.
- 5xx server errors: 500 Internal Server Error, 503 Service Unavailable. A 5xx caused by bad input is itself a defect, because the server should have returned a 4xx.
Common Gaps in API Test Coverage
- Checking only that the status is 200 without reading the body.
- Never sending a request without a token, or with another user's token, to confirm access control.
- Ignoring headers, such as the response Content-Type or caching headers.
- Skipping schema validation, so a renamed field slips through unnoticed.
- Testing only valid input. Empty strings, very long values, wrong types and negative numbers often reveal bugs.
Practical habit: for every endpoint, write at least one positive test, one validation test and one authorization test before adding more detailed cases.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.