QA Interview Hub · Knowledge base · Quizzes · Українська

QA Fundamentals › Web and client-server

What are the main HTTP methods and status codes?

HTTP methods express the client's intent, and the status code sums up the result: 2xx success, 3xx redirection, 4xx client error, 5xx server error. GET reads, POST creates, PUT replaces a resource fully, PATCH changes part of it, DELETE removes it. Tests check the exact code (for example 201 with Location on creation), not just 2xx, and tell 401 (identity unknown) from 403 (no permission).

QA Fundamentals · Web and client-server

POST returned 200 instead of 201, and a repeated DELETE gave 404. Bugs or normal?

An automated test for creating an order checks "status 2xx" and is green. A month later another team connects a client to this API, and the client expects 201 and a Location header to open the page of the new resource. The client breaks, because the server returned 200 without Location. The test did not lie: it just checked too little. This lesson is about what methods and status codes say, why they were agreed this way and what exactly to check from them.

How it works and why. The HTTP specification (RFC 9110) describes a method as the client's intent together with guarantees. Safe: the method only reads and does not change the server state. Idempotent: N identical requests leave the server in the same state as one. Cacheable: the response may be stored in a cache. These are not academic terms. Browsers, proxies, load balancers and HTTP clients rely on them: a GET can be cached, prefetched and safely repeated after a connection drop, while a POST must not be retried automatically, because it might create two orders. Hence a practical rule: "delete through a GET link" is a real bug, because a search robot or a prefetch will follow the link and delete data.

MethodPurposeSafeIdempotentTypical success
GETGet a resourceyesyes200
POSTCreate a resource or run an actionnono201 + Location, or 200/202
PUTReplace a resource entirelynoyes200 or 204 (201 if created)
PATCHChange part of a resourcenonot guaranteed200 or 204
DELETEDelete a resourcenoyes204 (or 200)

A status code is a summary: the first digit sets the class, so a client that does not know a code still understands its meaning. 2xx means success: 200 OK, 201 Created (the resource was created), 204 No Content (success without a body). 3xx is redirection: 301, 302, 304 Not Modified (the cache is still valid). 4xx is an error on the request side: 400 Bad Request, 401, 403, 404, 409 Conflict (the state of the resource contradicts the request), 422 Unprocessable Content (the syntax is right, the content failed the rules), 429 Too Many Requests. 5xx is a server error: 500, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout.

The two codes mixed up most often

401 Unauthorized

Who are you? The system does not know you

The token is missing, expired or forged. Fixed by logging in or refreshing the token. The response usually has a WWW-Authenticate header.

403 Forbidden

I know who you are, but I do not allow it

The user is authenticated, but the role or permissions are not enough. Logging in again changes nothing, another role or access is needed.

Figure 1. The name 401 is misleading: by meaning it is "not authenticated". If they are mixed up, the client tries to log in again where another role is needed.

Example: from "expect success" to a check that catches mistakes. We create an order and look at the response:

POST /api/orders HTTP/1.1
Authorization: Bearer <token>
Content-Type: application/json

{"sku": "A-100", "qty": 2}

HTTP/1.1 201 Created
Location: /api/orders/8412
Content-Type: application/json

{"id": 8412, "sku": "A-100", "qty": 2, "status": "new"}

A test "status 2xx" will pass both a 200 without Location and a 201 with a broken body. A more exact scenario uses the semantics of the methods:

Before: POST /api/orders → "a successful response"

After:
1. POST /api/orders → 201 Created, a Location header, an id in the body
2. GET by Location → 200, the data matches what was sent
3. The same POST again → a second resource is created or 409,
   as the contract says (POST is not idempotent)
4. DELETE /api/orders/8412 → 204; a repeated DELETE → 404
   (the state is the same: the order is gone; the responses may differ)
5. GET of the deleted one → 404
6. POST without a token → 401; someone else's resource → 403 (or 404 if hidden)

Item 4 often causes an argument. Idempotency is about the server state, not the response text: after the first DELETE the order is gone, after the second it is still gone, so the method is idempotent even though the second call returned 404. It is not a bug if the contract describes the behaviour this way. A bug begins where a repeated PUT or DELETE changes something: for example, the second DELETE removes the "next" order, or a PUT raises a version counter every time without changing the data.

Limits and alternatives. Real APIs deviate from the ideal, so the final judge is the contract of this particular API (Swagger/OpenAPI, documentation). Some APIs return 200 with an error in the body, which is convenient for novice clients and harmful for monitoring, because failures are not visible in the code statistics. GraphQL usually answers 200 and puts errors in an errors field. Teams draw the line between 400 and 422 differently, and whether to answer 403 or 404 for someone else's resource depends on whether its existence must be hidden. PATCH is idempotent only depending on the operation: "set status = paid" is safe to repeat, "add 10 to the balance" is not. To make a repeated POST safe (a network failure in the middle of a payment), an Idempotency-Key header is used: by the key the server recognises a repeat and does not run a second action. This is a common practice, but it is an agreement between the client and a particular API, not a part of basic HTTP.

Common mistakes.

  • Checking only "2xx" or only "not 500": the test misses 200 instead of 201, a missing Location and wrong error codes.
  • Assuming a 4xx is always a defect: 400, 404 or 409 for an incorrect request is correct behaviour that must be tested too.
  • Mixing up 401 and 403, and expecting 500 where the right answer is 400 or 422: a client error must not be shown by the server as its own failure.
  • Demanding an identical response from an idempotent method instead of an identical state.
  • Not checking side effects: after 204 on DELETE the resource must be gone, and after 201 on POST it must appear.

Check yourself

The first DELETE /orders/7 returned 204, the second 404. Is idempotency violated?

Answer

No. Idempotency is about the server state: after both calls order 7 is gone. A different response to a repeat is allowed and often intended. A violation would be if the repeat changed something: deleted another resource or changed counters.

A request without a token returned 403, and a request from a user without rights to an admin endpoint returned 401. What is wrong here?

Answer

The codes are swapped. Without a token the system does not know who it is, so 401 is expected. The user is known but has no rights, so 403 is expected. Consequence: in the second case the client will try to refresh the token and will not understand why it does not help.

Why is PATCH not guaranteed to be idempotent while PUT is?

Answer

PUT replaces the resource with a full new version, so a repeat gives the same result. PATCH describes a change: "set the field" is safe to repeat, while "increase the field by 10" or "add an item to an array" changes the state every time. So for PATCH idempotency is checked per operation.

In an interview. Junior: GET gets, POST creates, PUT replaces entirely, PATCH changes partially, DELETE deletes. Codes: 2xx success, 3xx redirection, 4xx client error, 5xx server error. The difference between 401 and 403: in the first case the system does not know who you are, in the second it knows, but you have no rights. Middle/Senior: I tie checks to semantics: GET is safe and does not change state, PUT and DELETE are idempotent by state, POST is not, so I test a repeated POST separately (a duplicate or 409, Idempotency-Key). For POST I expect 201 and Location, for validation errors 400 or 422 by the contract, not 500. I judge idempotency by the server state, not by the response text, and I tell 502 and 504 (a gateway) from 500 (the application), because it decides where to look for the cause.

More articles in this chapter