QA Interview Hub · Knowledge base · Quizzes

API

HTTP methods and idempotency

HTTP methods, idempotency, PUT vs PATCH

The main methods: GET retrieves a resource, POST creates, PUT replaces completely, PATCH changes partially, DELETE removes. HEAD also returns only headers, and OPTIONS returns the allowed methods, which CORS preflight uses. Safe methods don't change the server state: GET, HEAD, OPTIONS.

An idempotent method is one where several identical requests give the same server state as one. GET, PUT and DELETE are idempotent, POST isn't: two POSTs create two records. The response may differ, for example a repeated DELETE returns 404 instead of 204.

PUT sends the full representation of a resource, and fields absent from the body may be reset. PATCH sends only the fields that change. PATCH isn't guaranteed to be idempotent, for example the operation "increase the counter by 1". This is worth checking in tests: a repeated PUT must not create duplicates.

The methods table

An idempotent method is one where a repeated identical request doesn't change the server state beyond the first. A safe one is one that doesn't change data at all.

MethodWhat forBodyIdempotentTypical success code
GETRetrieve a resource or a listNoYes (and safe)200
POSTCreate a resource or perform an actionYesNo201
PUTReplace a resource completelyYesYes200 or 204
PATCHChange individual fieldsYesNot guaranteed200
DELETERemove a resourceUsually noYes204 or 200
HEADLike GET but only headersNoYes200
OPTIONSWhich methods are allowed; CORS preflightNoYes204

Idempotency concerns the server state, not the response. A repeated DELETE may return 404 instead of 204, but the resource is deleted all the same, so the method remains idempotent. HEAD is useful when you need to check that a resource exists or a file's size without downloading the body.

In an interview: GET, PUT, DELETE, HEAD and OPTIONS are idempotent, POST isn't, and PATCH is not guaranteed. Safe methods, like GET and HEAD, don't change data at all.

PUT vs PATCH and Idempotency-Key in fintech

PUT vs PATCH. PUT sends the whole object, and a field you didn't pass may become empty. PATCH changes only the passed fields. This is a ready test case: do a PUT without one field and check through GET what happened to it. If the field was wiped and the documentation says PUT is a full replacement, the behaviour is correct; if the API behaves like PATCH, it is a deviation from the contract.

Fintech. POST is non-idempotent, so pressing "Pay" again or repeating a request after a network timeout may debit money twice. The protection is the Idempotency-Key header: the client generates a unique key, the server remembers it and on a repeat returns the same result without creating a second payment.

How I test it:

  1. Send POST /transfers with the key K1, get 201 and the transfer id.
  2. Send the same request with the same K1.
  3. Check that the response contains the same id, and that in the database or history there is one transaction.
  4. Send a different body with the same K1 and check that the server returns an error (often 409 or 422), not quietly executing a new payment.
In an interview: PUT replaces a resource entirely, PATCH changes only the passed fields, and I check this with a GET after a PUT without one field. For payments POST is protected by Idempotency-Key, and I test that a repeat with the same key doesn't create a second transaction.