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

API › HTTP methods and idempotency

The methods table

GET, HEAD and OPTIONS are safe and idempotent, PUT and DELETE are idempotent, POST is not, and PATCH is not guaranteed. Typical success: GET 200, POST 201 with Location or 202 for asynchronous work, PUT 200 or 204, DELETE 204. An unsupported method should return 405 with an Allow header, not 404 or 500. The table gives typical expectations; the reference is the service contract.

API · Methods

Which code to expect from PUT and which from DELETE? A table worth knowing by heart.

Test cases for an endpoint begin with "which response counts as correct". Each method has settled expectations: what to return, whether there is a body, whether it can be repeated, whether it is cached. Knowing this table saves time when reviewing documentation and catches mistakes before the first run.

The table

MethodPurposeRequest bodySafeIdempotentTypical success
GETGet a resource or a listNoYesYes200
HEADLike GET, without the bodyNoYesYes200
OPTIONSCapabilities, CORS preflightNoYesYes200 or 204
POSTCreate a resource or run an actionYesNoNo201 or 200, 202
PUTReplace a resource fullyYesNoYes200 or 204 (201 if created)
PATCHChange some fieldsYesNoNot guaranteed200 or 204
DELETERemove a resourceUsually noNoYes204, 200 or 202

Why the codes are what they are

A success code is not arbitrary, it tells the client what happened. 201 Created says a new resource appeared, and a Location with its address goes along. 204 No Content says "done, no body": typical for DELETE and PUT, which have nothing to return. 202 Accepted says "accepted, processing continues": the answer to a long operation, the client gets the result later. 200 is kept for cases where the response has a body with the result.

So the same action in different APIs may correctly give different codes. A PUT to an existing resource gives 200 or 204, and a PUT to a new address, where the API allows it, gives 201. That is not a defect as long as the code matches the documentation and is the same across the service's endpoints.

Example: checking the table on a live API

HEAD is useful to check existence and size of a file without downloading it, and OPTIONS shows what a resource supports:

curl -I "$BASE/v1/files/9" -H "Authorization: Bearer $TOKEN"
# 200, Content-Length: 1048576, no body
curl -i -X OPTIONS "$BASE/v1/orders"
# 204, Allow: GET, POST, OPTIONS

Two tests that take a few minutes to add to a suite:

test('HEAD matches GET headers and has no body', async ({ request }) => {
  const get = await request.get('/v1/files/9');
  const head = await request.head('/v1/files/9');
  expect(head.status()).toBe(get.status());
  expect(head.headers()['content-length']).toBe(get.headers()['content-length']);
  expect((await head.body()).length).toBe(0);
});

test('unsupported method gives 405 with Allow', async ({ request }) => {
  const res = await request.delete('/v1/orders');
  expect(res.status()).toBe(405);
  expect(res.headers()['allow']).toContain('GET');
});

The first checks the HEAD contract: the same status and headers as GET, but no body. The second catches a classic mistake where the server returns 404 or 500 for an unsupported method instead of 405 Method Not Allowed with an Allow header. RFC 9110 requires this header in a 405.

Idempotency and the response

Server state

After the first and the second DELETE the resource is gone. That is what idempotency is about.

Response code

204 the first time, 404 the second. That is allowed: the code may differ, the state may not.

Figure 1. Two different things that are easy to mix up: idempotency belongs to the state, not to the code.

Limits of the table

  • The table describes typical expectations. A real API may return 200 with a body on DELETE (the deleted object) or 202 for an asynchronous delete. The reference is the service contract.
  • The "idempotent" mark is the intent of the method too: a server can implement PUT so that it is not. A test checks exactly that.
  • Not every API supports HEAD and OPTIONS. If a method is not implemented, we expect 405, not 500.
  • Some caches and clients cache only GET and HEAD. Responses to POST are cached rarely, only with explicit headers.

Common mistakes

  • Expecting 200 for everything. A test that checks only "success" misses a 201 without Location or a 204 with a body.
  • Comparing a DELETE repeat by code. It may differ, so the state is compared through GET.
  • Demanding a body from HEAD. By definition there is none, and the headers should match GET.
  • Mixing "safe" and "idempotent". PUT and DELETE change state, so they are unsafe, yet idempotent.

Check yourself

Which code to expect from POST /orders and what else to check in the response?

Answer

201 Created (200 or 202 depending on the contract). I also check Location or the id in the body, then a GET at that address: the resource must exist.

The server returns 404 for DELETE /orders (a collection). Is that right?

Answer

No, 405 Method Not Allowed with an Allow header would be more precise: the resource exists but the method is not supported for it. 404 misleads, and 500 is a bug.

Why is PUT called unsafe but idempotent?

Answer

A safe method does not change state. PUT does, so it is unsafe. Idempotency says a repeat of the same PUT changes nothing beyond the first. These are two independent properties.

In an interview. Junior: GET, HEAD, OPTIONS are safe and idempotent, PUT and DELETE are idempotent, POST is not, PATCH is not guaranteed. Success: GET 200, POST 201, DELETE 204. Middle/Senior: A success code carries meaning: 201 with Location, 204 without a body, 202 for asynchronous processing. An unsupported method must give 405 with Allow. Idempotency belongs to the state, so a DELETE repeat with 404 is fine, and PATCH depends on the operation. I use the table as a starting grid and take the reference from the service contract.

More articles in this chapter