Response Codes: 401, 404, 405 and an Empty List
How the server reports problems: 401 without logging in, 405 for a method that is not allowed, 404 when deleting a product that does not exist, and 200 OK with an empty list.
About the product
Hi! We’ve seen how everything works when it goes well. But a tester also checks how the server reports problems: every response has a status code for that. Today we’ll go through the most common codes using examples from the BB Food admin panel.
Great! Where do we start?
Kadri is already waiting for you in the meeting room and will show you everything. Today there are four cases: 401, 405, an empty list with code 200, and deleting a product that no longer exists.
Hi! Every server response has a status code. You’ve already seen 200, 201 and 204. The first digit of the code tells you what happened.
Hi Kadri! What does the first digit mean?
Codes are split into groups by the first digit. Remember the four main ones:
Status codes
2xx— success: the request was carried out (200 OK,201 Created,204 No Content)3xx— a redirect: the resource has moved. For example, the old deals page/salewas removed and the deals now live at/promo. ToGET /salethe server answers301 Moved Permanently, puts/promoin theLocationheader, and the browser goes there on its own4xx— a client error: the request is wrong or can’t be carried out (401,404,405)5xx— a server error: the client did everything right, but something broke on the server (500,503)
Let’s check you’ve got it. The server answered with code 404. Whose error is it?
Let’s go through examples!
Application under test
Example 1
The first example. An administrator opened the admin panel, but is not logged in. They try to add a product anyway.
And what will the server answer?
The client will send a request to add a product, but it won’t contain proof that a logged-in administrator sent it. Press “Add product” and see what happens.
Request
POST /api/v1/products
{
"name": "Cottage cheese, 200 g",
"price": 1.89
}- the method and the address are the same as for a successful add:
POST /api/v1/products - the request body is the new product’s data, and it is correct
- there is no proof of login: the server doesn’t know who sent the request
The animation is slowed down: in reality the whole trip takes a few dozen milliseconds.
| id | name | price |
|---|
No products loaded yet.
The server answered with 401 Unauthorized — “not authorized”. It doesn’t know who sent the request, so it refused before even going to the database. The database took no part in this scenario, and the table stayed the same.
Response
401 Unauthorized
{
"error": "Unauthorized"
}- the status code — how it went.
401 Unauthorizedmeans “log in” - the response body — an error message, not a product
What a tester checks: the code is 401, the product was not added to the database, and the user sees a clear message and can log in again.
What does code 401 mean?
Got it! And what’s the next code?
Example 2
The second example. The client developer added a “Delete all products” button to the admin panel, but made a mistake: it sends DELETE to the address of the whole list, and the server doesn’t support that action.
Will it work?
The button will send DELETE to the address /api/v1/products, that is, to the whole list rather than to one product. The client got it wrong, but the server doesn’t trust the client and checks every request itself. Press “Delete all products” and see what the server answers.
Request
DELETE /api/v1/products
- the method — what to do.
DELETEmeans “delete” - the address —
/api/v1/products: the whole list, no product number - no body
The server answered with 405 Method Not Allowed — “the method is not allowed”. The address exists, but the server supports only GET and POST for it. The database took no part, and all the products are still there.
Response
405 Method Not Allowed
{
"error": "Method Not Allowed"
}- the status code —
405 Method Not Allowedmeans “this method is not allowed for this address” - the response body — an error message
What a tester checks: the code is 405, the data in the database didn’t change, and the user sees a clear message. They also file a bug against the client: the button sends a request the server doesn’t support. Don’t confuse it with 404: there the address or resource itself is not found.
The server allows only GET and POST for the address /api/v1/products. The client sent DELETE. Which code will the server return?
And what if everything worked, but there is no data?
Example 3
The third example is trickier. The shop has just opened, and the database has no products yet. A customer opens the catalogue.
Will the server return an error?
The client will request the product list, just like in the very first task. Look at the table in the database: it is empty. Press “Load products” and see what the server answers.
Request
GET /api/v1/products
- the method and the address are the same:
GET /api/v1/products - no body
- the
productstable has no rows at all
The server answered with 200 OK, and the body is an empty list []. This is not an error: the request was carried out successfully, there is just no suitable data. The server found 0 rows in the database and honestly returned an empty list.
Response
200 OK []
- the status code —
200 OKmeans “the request succeeded” - the response body — an empty list
[]: there are no products, but no error either
What a tester checks: the code is 200, not 404 or 500, the body is an empty list [], and the site shows a clear hint like “No products yet” rather than a blank screen or an error.
The server returned 200 OK and an empty list []. Is that an error?
And what happens if I delete a product that no longer exists?
Example 4
And the last example. Two administrators work at the same time. One has already deleted the product “Milk 2.5%, 1 l”, and the other’s page hasn’t refreshed, so the product is still in the list.
And what happens if I delete it again?
Note that the database table no longer has the product with id 3, while the client’s list still shows it. Press “Delete “Milk 2.5%, 1 l”” and see what the server answers.
Request
DELETE /api/v1/products/3
- the method and the address are as in a normal deletion:
DELETE /api/v1/products/3 - no body
- the product with
id3 is no longer in the database
The server answered with 404 Not Found — “not found”. The database didn’t find the row with id 3, deleted nothing and told the server: 0 rows deleted. With nothing to delete, the server honestly said there is no such product. Note that the repeated DELETE didn’t change the database — the product is gone after both the first and the second request. Only the response code differs: 204 the first time and 404 the second. So, unlike POST, repeating DELETE creates no duplicates and doesn’t spoil the data.
Response
404 Not Found
{
"error": "Product not found"
}- the status code —
404 Not Foundmeans “there is no such resource” - the response body — an error message
What a tester checks: the code is 404 instead of 204, the database didn’t change, and the user sees a clear message, for example “Product not found: it may have been deleted already”.
What will the server return when you delete a product that no longer exists?
I think I’ve got it!
Great! Now you can read response codes. Remember: 401 — not logged in, 405 — the method is not allowed, 404 — not found, and 200 with an empty list — not an error. A tester checks the code, the data in the database, and the message the user sees.
Quick reference
Code groups
The first digit of the code tells what happened. 2xx is success, 4xx is a client error (a wrong or impossible request), 5xx is a server error.
401 Unauthorized
The server doesn’t know who sent the request: the client is not logged in or sent invalid login data. A tester checks that the data didn’t change and the user sees a clear message.
405 Method Not Allowed
The address exists, but this method is not allowed for it.
404 Not Found
There is no such resource: for example, the product is already deleted. The server changes nothing and says the thing asked for isn’t there.
200 OK and an empty list
An empty result is not an error. The request succeeded, there is just no suitable data. A tester checks code 200, an empty list in the body and a clear hint for the user.