HTTP methods
HTTP-методы
Every HTTP request has a method that tells the server what the client wants to do. For example, GET asks for data, while DELETE asks to remove the resource at the requested address.
Also known as: GET, POST, PUT, DELETE
One address, different requests
The URL identifies the target of an HTTP request. The method describes what to do with it. A backend can route GET /tasks/42 and DELETE /tasks/42 to different handlers even though the path is the same. Clients, proxies, and caches also rely on the method's meaning, so it isn't just a label for application code.
Choosing a method
GETretrieves a representation of the target resource, such as a task encoded as JSON.POSTsubmits data for the target resource to process. Creating a task in a collection is one use; starting an export is another.PUTsets the state of the resource at a known address. Think of replacement rather than a merge of whichever fields were sent. An API can also allowPUTto create a resource at that address.PATCHapplies a set of changes. The API defines how to express those changes: setting a value and incrementing a value are different instructions.DELETEasks the server to remove the resource at that address. The server might keep a record internally rather than erase it from its database.HEADis likeGETwithout a response body. It's useful when you only need headers, such as anETag.OPTIONSasks about communication options and is used by browsers for CORS preflight requests.
Example: a task API
POST /tasks → 201 Created (Location: /tasks/42)
GET /tasks/42 → 200 OK
PATCH /tasks/42 → 200 OK
DELETE /tasks/42 → 204 No Content
Here, the server picks the address of the new task. A different API might let clients choose the address and send a PUT directly to it. The method doesn't dictate a single response code: a successful PATCH can return the updated task with 200 OK or no body with 204 No Content. If the server recognizes a method but the target resource doesn't support it, 405 Method Not Allowed comes with an Allow header listing supported methods.
Two properties worth keeping separate
A safe method doesn't ask for a state change. Logging a GET request is fine; closing a task in response to GET isn't. Safety here is about the operation's meaning, not whether authentication is required.
An idempotent method has the same intended effect whether an identical request is sent once or several times. Safe methods are idempotent, and so are PUT and DELETE. The responses needn't match: a deletion can succeed with 204 and a repeat can return 404, with the task absent either way. Neither POST nor PATCH promises idempotency at the method level.
How it comes up in interviews
Expect a retry scenario: your client sends POST /tasks and times out. A timeout says that the client didn't get a response, not that the server did nothing. If the API supports idempotency keys, the client can reuse the same key on a retry so the server can recognize the original operation instead of creating a second task.