Skip to content
Sign inGet all access
Chapter 3·12 minFree

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: close
OutputHTTP/1.1 200 OK

200 is not its only success. Range asks for part of a file:

GET / HTTP/1.1
Host: localhost
Range: bytes=0-9
Connection: close
OutputHTTP/1.1 206 Partial Content

A conditional GET returns no body when nothing changed:

GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: close
OutputHTTP/1.1 304 Not Modified

Still safe, still a GET — chapter 6.

The same request, headers only — byte-identical to GET’s:

HEAD / HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615

It answers for any path, so it is the cheapest existence check:

HEAD /nope HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 404 Not Found

233 bytes against GET’s 848:

terminal
printf '%s\r\n' 'HEAD / HTTP/1.1' 'Host: localhost' \
    'Connection: close' '' | nc localhost 8080 | wc -c
Output$ printf '%s\r\n' 'HEAD / HTTP/1.1' 'Host: localhost' \ 'Connection: close' '' | nc localhost 8080 | wc -c233$ printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' \ 'Connection: close' '' | nc localhost 8080 | wc -c848

POST

Submit data and let the server decide what it means:

terminal
nc -l 9000 &
curl -s -X POST -d 'name=ada' localhost:9000/users
OutputPOST /users HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*Content-Length: 8Content-Type: application/x-www-form-urlencodedname=ada

curl added Content-Length and Content-Type for the body — chapter 5.

PUT versus POST

PUT sends a whole resource to an address you chose:

terminal
nc -l 9000 &
curl -s -X PUT -d 'name=ada' localhost:9000/users/1
OutputPUT /users/1 HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*Content-Length: 8Content-Type: application/x-www-form-urlencodedname=ada

The 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:

terminal
nc -l 9000 &
curl -s -X PATCH localhost:9000/users/1 \
  -H 'Content-Type: application/merge-patch+json' \
  -d '{"name":"ada"}'
OutputPATCH /users/1 HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*Content-Type: application/merge-patch+jsonContent-Length: 14{"name":"ada"}

DELETE

No body. The path is the whole request:

terminal
nc -l 9000 &
curl -s -X DELETE localhost:9000/users/1
OutputDELETE /users/1 HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*

OPTIONS

Ask what a resource supports. A browser sends it before a cross-origin request:

terminal
nc -l 9000 &
curl -s -X OPTIONS localhost:9000/api \
  -H 'Origin: https://app.example.com' \
  -H 'Access-Control-Request-Method: PUT'
OutputOPTIONS /api HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*Origin: https://app.example.comAccess-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:

terminal
for i in 1 2; do
  printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' \
      'Connection: close' '' | nc localhost 8080 | grep -i '^etag'
done
OutputETag: "67ffa8c6-267"ETag: "67ffa8c6-267"

The 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: close
OutputHTTP/1.1 405 Not Allowed
DELETE / HTTP/1.1
Host: localhost
Content-Length: 0
Connection: close
OutputHTTP/1.1 405 Not Allowed

A 405 is supposed to list what is permitted. nginx sends no such header:

PUT / HTTP/1.1
Host: localhost
Content-Length: 0
Connection: close
OutputHTTP/1.1 405 Not AllowedServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 157Connection: close