HTTP Methods
The HTTP method (or verb) declares what a request wants to do with a resource — fetch it, create one, update it, delete it. Methods are the backbone of REST API design, and two of their properties — safety and idempotency — have real consequences for retries, caching, and correctness. This page covers each method, what it means, and the rules that aren't obvious until they bite you.
Reading the method in Node
import { createServer } from 'node:http'
createServer((req, res) => {
switch (req.method) {
case 'GET': return handleGet(req, res)
case 'POST': return handlePost(req, res)
case 'PUT': return handlePut(req, res)
case 'DELETE': return handleDelete(req, res)
default:
res.writeHead(405, { Allow: 'GET, POST, PUT, DELETE' })
res.end('Method Not Allowed')
}
}).listen(3000)The common methods
Method | Purpose | Has body? |
|---|---|---|
| Retrieve a resource | No |
| Create a resource / submit data | Yes |
| Replace a resource entirely | Yes |
| Partially update a resource | Yes |
| Remove a resource | Sometimes |
| Like GET but headers only, no body | No |
| Ask what methods/CORS are allowed | No |
Safe vs idempotent — the two properties that matter
These two terms are constantly confused, but they're distinct and important:
Safe = the request does not change server state (read-only).
GET,HEAD,OPTIONSare safe.Idempotent = making the request N times has the same effect as making it once.
GET,PUT,DELETE,HEADare idempotent.POSTis neither safe nor idempotent — two POSTs typically create two resources.PATCHis generally not guaranteed idempotent (depends on the patch semantics).
Method | Safe? | Idempotent? |
|---|---|---|
| Yes | Yes |
| No | Yes |
| No | Yes |
| No | No |
| No | Not necessarily |
PUT vs PATCH
Both update, but differently. PUT replaces the whole resource with the body you send; PATCH applies a partial change. Sending a PUT with only some fields can wipe the omitted ones:
Resource: { "name": "Ada", "email": "ada@x.com", "role": "admin" }
PUT /users/1 { "name": "Ada Lovelace" }
→ replaces entirely → { "name": "Ada Lovelace" } ← email & role GONE
PATCH /users/1 { "name": "Ada Lovelace" }
→ merges → { "name": "Ada Lovelace", "email": "ada@x.com", "role": "admin" }GET vs POST and bodies
HEAD and OPTIONS
HEADreturns the same headers asGET(includingContent-Length) but no body — useful to check if a resource exists, its size, or itsLast-Modifiedwithout downloading it.OPTIONSasks the server what it allows. Browsers send an automatic OPTIONS preflight before certain cross-origin requests (CORS) to check permissions.
RESTful mapping
Action | Method + path |
|---|---|
List users |
|
Get one user |
|
Create a user |
|
Replace a user |
|
Partially update |
|
Delete a user |
|