Methods
What you are asking the server to do, and the three properties that matter.
The method is the verb. Nine exist; four matter.
GET
Ask for a resource. No body, and nothing changes:
GET / HTTP/1.1
Host: localhost
Connection: close200 is not its only success. Range asks for part of a file:
GET / HTTP/1.1
Host: localhost
Range: bytes=0-9
Connection: closeA conditional GET returns no body when nothing changed:
GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: closeStill safe, still a GET — chapter 6.
HEAD
The same request, headers only — byte-identical to GET’s:
HEAD / HTTP/1.1
Host: localhost
Connection: closeIt answers for any path, so it is the cheapest existence check:
HEAD /nope HTTP/1.1
Host: localhost
Connection: close233 bytes against GET’s 848:
printf '%s\r\n' 'HEAD / HTTP/1.1' 'Host: localhost' \
'Connection: close' '' | nc localhost 8080 | wc -cPOST
Submit data and let the server decide what it means:
nc -l 9000 &
curl -s -X POST -d 'name=ada' localhost:9000/userscurl added Content-Length and Content-Type for the body — chapter 5.
PUT versus POST
PUT sends a whole resource to an address you chose:
nc -l 9000 &
curl -s -X PUT -d 'name=ada' localhost:9000/users/1The bytes differ only in method and path:
POST /users |
PUT /users/1 |
|
|---|---|---|
| Who picks the address | the server | you |
| Sent twice | two users, maybe | one user, same state |
| Body | whatever the endpoint takes | the whole resource |
PUT replaces, so an omitted field is cleared rather than left alone. That is why PATCH
exists.
PATCH
A partial update. The body describes the change, not the resource:
nc -l 9000 &
curl -s -X PATCH localhost:9000/users/1 \
-H 'Content-Type: application/merge-patch+json' \
-d '{"name":"ada"}'DELETE
No body. The path is the whole request:
nc -l 9000 &
curl -s -X DELETE localhost:9000/users/1OPTIONS
Ask what a resource supports. A browser sends it before a cross-origin request:
nc -l 9000 &
curl -s -X OPTIONS localhost:9000/api \
-H 'Origin: https://app.example.com' \
-H 'Access-Control-Request-Method: PUT'That is a CORS preflight — chapter 11.
Safe, idempotent, cacheable
Three properties, and what the names actually mean:
| Method | Safe | Idempotent | Cacheable |
|---|---|---|---|
GET |
yes | yes | yes |
HEAD |
yes | yes | yes |
OPTIONS |
yes | yes | no |
PUT |
no | yes | no |
DELETE |
no | yes | no |
POST |
no | no | rarely |
PATCH |
no | no | no |
Safe: nothing changes. Idempotent: sending it again lands in the same state. Cacheable: the response may be reused.
Safe implies idempotent. Two identical GETs return the same ETag:
for i in 1 2; do
printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' \
'Connection: close' '' | nc localhost 8080 | grep -i '^etag'
doneThe reverse does not hold — DELETE changes things, and repeating it does not.
What the server allows
nginx serves files, so everything but GET and HEAD is refused — including OPTIONS:
OPTIONS / HTTP/1.1
Host: localhost
Connection: closeDELETE / HTTP/1.1
Host: localhost
Content-Length: 0
Connection: closeA 405 is supposed to list what is permitted. nginx sends no such header:
PUT / HTTP/1.1
Host: localhost
Content-Length: 0
Connection: close