QA AND TEST AUTOMATION · PART 4 · API TESTING · BASICS · 38 OF 112

HTTP and REST for API Testers: Methods, Status Codes

Everything a tester needs to read and test an HTTP API: requests and responses, methods (GET, POST, PUT, PATCH, DELETE), safe and idempotent, status codes and what each family means, headers, JSON bodies, REST resources and URLs, and what to check in every API test. All shown with real requests to the ShopRung API and its real responses.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Request and response
  • safe and idempotent
  • status families
  • headers
  • REST
  • check status, body, side effects

Notes

Request and response

Every HTTP call is a request and a response. The request has a method, here GET, a path, headers like the host, and sometimes a body. The response has a status line, here 200 OK, headers like the content type, and a body. Ask ShopRung for product one and the body is JSON: the Clean Code Notebook, in Books, twelve fifty, forty in stock. An API test sends requests like this and checks every part of the response.

One real exchange
GET /api/products/1 HTTP/1.1
Host: localhost:8080

HTTP/1.1 200 OK
Content-Type: application/json

{"id":1,"sku":"BK-101",
 "name":"Clean Code Notebook",
 "category":"Books",
 "priceCents":1250,"price":
 "$12.50","stock":40,"inStock":true}

Methods

The method says what the client wants. GET reads, like listing products. POST creates something or triggers an action, like adding to the cart or placing an order. PATCH changes part of something, like a cart line's quantity. PUT replaces a whole resource; ShopRung does not use it. DELETE removes, like a cart line. Use a method the API does not allow, like DELETE on a product, and ShopRung answers four hundred and five, method not allowed.

Methods in ShopRung
GET    /api/products       read
POST   /api/cart/items     create
                           or act
PATCH  /api/cart/items/1   change
                           part
PUT    (replace whole)     not used
DELETE /api/cart/items/1   remove

wrong method → 405
  DELETE /api/products/1

Safe and idempotent

Two properties matter to testers. A safe method does not change data: GET must never alter anything. An idempotent call gives the same result whether you make it once or twice: setting a cart line to two with PATCH, twice, still leaves two. POST usually is not idempotent: adding four notebooks twice gives eight. Test it: call twice and compare. A GET that changes data, or a payment POST that charges twice on a retry, are serious bugs.

Safe and idempotent
  • safe: does not change data GET (and HEAD, OPTIONS)
  • idempotent: doing it twice = doing it once GET, PUT, DELETE, and PATCH quantity=2 here
  • NOT idempotent: POST add 4 notebooks twice → 8 in the cart
  • test: call twice, compare

Status codes

Status codes come in families. Two hundreds mean success. Three hundreds mean redirect. Four hundreds mean the client did something wrong: a bad request, not logged in, not allowed, not found. Five hundreds mean the server failed. A useful rule: when your test sends bad input, expect a four hundred something with a clear message. If the server answers five hundred, that is almost always a bug, because bad input should never crash a server.

Status code families
  • 2xx success 200 OK, 201 Created, 204 No Content
  • 3xx redirect (301, 302, 304)
  • 4xx the CLIENT is wrong 400, 401, 403, 404, 405, 409, 422, 429
  • 5xx the SERVER failed 500, 502, 503, 504
  • a 5xx in a test = usually a bug

Codes in ShopRung

ShopRung uses each code precisely, and tests should check that. Two hundred and one when an order is created. Four hundred for a quantity of zero or a body that is not JSON. Four oh one with no login token. Four oh three when a customer tries to ship an order. Four oh four for a product that does not exist. Four oh five for a method the path does not support. Four oh nine for conflicts, like asking for more stock than exists, or paying an order twice.

Real ShopRung answers
201 POST /api/orders (placed)
400 quantity 0: invalid_quantity
400 body "not json": bad_request
401 no token: unauthorized
403 customer ships: forbidden
404 /api/products/999
405 DELETE /api/products/1
409 add 4 of 3 left:
      out_of_stock
409 pay a PAID order

Headers

Headers carry information about the message. On requests, content type says the body is JSON, authorization carries the login token, and accept says which format you want back. On responses, check the content type, caching headers, the location of a newly created resource where an API provides it, and security headers. And check what should not be there: no internal server versions, stack traces or secrets leaking in headers or bodies.

Headers testers check
  • request Content-Type: application/json Authorization: Bearer <token> Accept: application/json
  • response Content-Type: application/json Cache-Control, Location (201) security headers
  • test: right type, no secrets

REST

REST is a style for designing HTTP APIs. Resources are nouns at URLs: orders, and order seven. Methods are the verbs: GET order seven. Calls are stateless: each request carries its own authentication, so the server does not remember you between calls. Data travels as JSON representations. Actions that are not simple create, read, update or delete become sub resources, like posting to an order's cancel. Query strings filter lists, like question mark q equals mug.

REST in one screen
  • resources are nouns at URLs /api/orders, /api/orders/7
  • methods are the verbs GET /api/orders/7
  • stateless: each call carries its own auth (the token)
  • JSON representations
  • actions that do not fit CRUD: POST /api/orders/7/cancel
  • query strings filter: ?q=mug

What to check

What should every API test check? The exact status code. The body: the right fields, types and values, and for errors, both the error code and the message. Key headers. The side effects: after placing an order, read it back, check the stock went down, or query the database. That nothing else changed. The response time against an agreed limit. And the overall shape, with a JSON schema, which has its own episode in this part.

Every API test checks
  • status code (exact)
  • body: fields, types, values error code AND message
  • headers: content type
  • side effects: GET the order back, check stock, the DB
  • nothing else changed
  • time: under the agreed limit
  • schema: shape of the JSON

Negative tests

Most API bugs hide in requests the developer did not expect, because the UI never sends them. Try a missing field and an extra field. A string where a number belongs. An empty body and invalid JSON. Huge and negative values. A missing or broken token. Someone else's order id. The wrong method. The same request twice. Each should get a four hundred something with a clear message, never a five hundred and never silently wrong data.

Requests to try
  • missing field, extra field
  • wrong type: "4" instead of 4
  • empty body, invalid JSON
  • huge values, negative values
  • missing or broken token
  • someone else's id (ORD-7)
  • wrong method, wrong path
  • same request twice
  • expect 4xx + clear message

Common mistakes

API testing habits to drop. Checking only that the status is two hundred. Not checking error codes and messages. Never reading the data back to confirm a change. Testing only the happy path. Testing through the browser what the API could test in a fraction of the time. Ignoring headers. And trusting a two hundred status whose body actually says error, which some badly designed APIs do. Always check the body too.

Avoid these
  • checking only "status 200"
  • not checking error messages
  • never reading data back
  • only happy-path requests
  • testing through the UI what the API could test faster
  • ignoring headers
  • trusting 200 with an error inside the body

Say it like this

In the interview: I check the exact status code, the body's fields, values and schema, error codes and messages for bad input, key headers, side effects by reading the data back, and response time. Beyond the happy path, I test authentication and authorisation, idempotency by repeating calls, and negative inputs, expecting a four hundred something with a clear message, never a five hundred. Then name the codes you know: two oh one, four oh one versus four oh three, four oh nine.

In the interview
  • "The exact status code, the body's fields and values and its schema, error codes and messages for bad input, key
  • headers, side effects by reading data back, and response time. I also test
  • auth, idempotency and negative inputs, expecting
  • 4xx, never 5xx."

Interview questions

Q1What is the difference between PUT and PATCH?

PUT replaces the whole resource with the request body; PATCH changes only the fields sent. PUT is idempotent by definition; PATCH can be designed to be.

Q2What does idempotent mean? Which methods are idempotent?

Repeating the call has the same effect as making it once. GET, HEAD, PUT, DELETE and OPTIONS are idempotent; POST generally is not.

Q3What is the difference between 401 and 403?

401 Unauthorized means not authenticated (missing or invalid credentials); 403 Forbidden means authenticated but not allowed to do this.

Q4When would an API return 409?

For a conflict with the current state, such as paying an already-paid order or requesting more stock than is available.

Q5What does a 5xx status in a test usually mean?

A server-side failure, usually a bug: invalid input should produce a 4xx with a clear message, not crash the server.

Q6What is REST?

An architectural style where resources are identified by URLs, manipulated with standard HTTP methods, represented (often) as JSON, and requests are stateless.

Q7What do you validate in an API response?

Status code, body fields, types and values, schema, error codes and messages, headers, side effects and response time.

Q8What is the difference between 200 and 201?

200 OK is a general success; 201 Created signals that a new resource was created, often with a Location header or the new resource in the body.