What are the common HTTP methods, and what do safe and idempotent mean?
EasyConceptDNS, HTTP & HTTPS
Answer
HTTP (HyperText Transfer Protocol) is the request-response protocol of the web: the client sends a request whose method states the intended action, and the server returns a response.
- GET reads a resource, POST creates one or submits data for processing, PUT replaces a resource completely, PATCH updates part of it and DELETE removes it. HEAD (headers only) and OPTIONS (allowed methods) are also common.
- A safe method does not change anything on the server: GET, HEAD and OPTIONS.
- An idempotent method has the same effect whether it is sent once or many times: the safe methods plus PUT and DELETE. POST is not idempotent, since sending it twice may place two orders, and PATCH is not guaranteed to be.
- This matters for retries: clients and proxies can safely resend an idempotent request after a network error.
Example: Updating a user's email address:
PATCH /users/42 HTTP/1.1
Host: api.example.com
Content-Type: application/json
{"email": "asha@example.com"}
The server replies HTTP/1.1 200 OK. Sending DELETE /users/42 twice removes the user once; the second call returns 404, but the final state is the same.