API Testing · API Basics

HTTP Basics for Testers: Methods, Status Codes, Headers

Lesson 2 of 7 · 10 min read

Every API call is an HTTP request and a response. Once you can read both, API testing is mostly checking that the response is right for the request.

Parts of a request

POST https://jsonplaceholder.typicode.com/posts?draft=false
Content-Type: application/json
Authorization: Bearer eyJhbGciOi...

{
  "title": "My first post",
  "body": "Hello",
  "userId": 1
}
  • Method: POST, what you want to do.
  • URL: the endpoint, /posts. After ? come query parameters such as draft=false.
  • Headers: extra information, like the body format and who you are.
  • Body: the data you send, usually JSON. GET requests normally have no body.

HTTP methods

MethodMeaningExample
GETRead dataGET /users/1
POSTCreate something newPOST /posts
PUTReplace a whole recordPUT /posts/1
PATCHChange some fieldsPATCH /posts/1
DELETERemove a recordDELETE /posts/1

Path parameters are part of the URL, like the 1 in /users/1. Query parameters filter or sort: /posts?userId=1.

Status codes

CodeMeaningWhen you see it
200 OKSuccessGET, PUT, PATCH worked
201 CreatedA new record was madePOST worked
204 No ContentSuccess, nothing to returnOften after DELETE
400 Bad RequestYour data is wrongMissing or invalid field
401 UnauthorizedNot logged inNo token or a bad token
403 ForbiddenLogged in but not allowedA user tries an admin action
404 Not FoundIt does not existGET /users/99999
409 ConflictClashes with existing dataRegistering the same email twice
422 UnprocessableValid format, breaks a ruleQuantity of 0
429 Too Many RequestsRate limit hitToo many calls too fast
500 Server ErrorThe server crashedAlways a bug worth reporting

Quick memory trick: 2xx success, 4xx the client (you) did something wrong, 5xx the server did something wrong.

Response headers worth checking

  • Content-Type: application/json: the body really is JSON.
  • Location: after a 201, where the new record lives.
  • Cache-Control, CORS headers and security headers, for non-functional checks.

Try it with curl

curl -i https://jsonplaceholder.typicode.com/users/1

curl -i -X POST https://jsonplaceholder.typicode.com/posts \
  -H "Content-Type: application/json" \
  -d '{"title":"Hello","body":"First post","userId":1}'

-i shows the status line and headers. The second call should return 201 Created.

Tool: paste any response into the free JSON formatter to read it easily.
All 7 lessons in API Testing
  1. What Is API Testing?
  2. HTTP Basics for Testers: Methods, Status Codes, Headers
  3. API Testing with Postman, Step by Step
  4. What to Test in an API: A Complete Checklist
  5. Testing API Authentication: API Keys, Tokens and OAuth
  6. Automate API Tests with REST Assured (Java)
  7. API Test Framework Structure and CI