|
| 1 | +--- |
| 2 | +title: 'HTTP Semantics' |
| 3 | +source: 'https://www.rfc-editor.org/rfc/rfc9110' |
| 4 | +description: 'How HTTP methods express intent, safety, and idempotency in REST APIs.' |
| 5 | +tags: ['rest-api', 'http-methods', 'semantics'] |
| 6 | +--- |
| 7 | + |
| 8 | +# HTTP Semantics |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +HTTP methods are contracts. If method semantics are wrong, clients cannot trust your API behavior. |
| 13 | +Most production API bugs start from semantic mismatch, not syntax errors. |
| 14 | + |
| 15 | +This note focuses on method intent, safety, and idempotency. |
| 16 | + |
| 17 | +### Quick Takeaways |
| 18 | + |
| 19 | +- Method choice defines behavior expectations |
| 20 | +- Do not use `GET` for state changes |
| 21 | +- Idempotency is critical for retry-safe operations |
| 22 | + |
| 23 | +## Definition |
| 24 | + |
| 25 | +- **Safe methods:** do not change server state (`GET`, `HEAD`) |
| 26 | +- **Idempotent methods:** repeated call has same final state (`PUT`, `DELETE`) |
| 27 | +- **Non-idempotent methods:** repeated call may create different outcomes (`POST`) |
| 28 | + |
| 29 | +### Method Matrix |
| 30 | + |
| 31 | +- `GET`: safe yes, idempotent yes, common use is read resource |
| 32 | +- `POST`: safe no, idempotent no, common use is create action/session/resource |
| 33 | +- `PUT`: safe no, idempotent yes, common use is full replace intent |
| 34 | +- `PATCH`: safe no, usually non-idempotent, common use is partial update |
| 35 | +- `DELETE`: safe no, idempotent yes, common use is remove resource |
| 36 | + |
| 37 | +## The Analogy |
| 38 | + |
| 39 | +Pressing an elevator button: |
| 40 | + |
| 41 | +- `GET`: check current floor display |
| 42 | +- `POST`: call elevator for a new trip |
| 43 | +- `PUT`: set floor indicator to a specific value |
| 44 | +- `DELETE`: clear a scheduled stop |
| 45 | + |
| 46 | +Pressing once or five times should follow method semantics. |
| 47 | + |
| 48 | +## When You See It |
| 49 | + |
| 50 | +Common API design decisions: |
| 51 | + |
| 52 | +- create session or token -> `POST` |
| 53 | +- update one field -> `PATCH` |
| 54 | +- replace full resource -> `PUT` |
| 55 | +- remove resource -> `DELETE` |
| 56 | + |
| 57 | +## Examples |
| 58 | + |
| 59 | +**Good:** |
| 60 | + |
| 61 | +- `DELETE /v1/sessions/{id}` to revoke one session |
| 62 | + |
| 63 | +**Bad:** |
| 64 | + |
| 65 | +- `GET /v1/logout` that mutates session state |
| 66 | + |
| 67 | +**Good snippet (Express style):** |
| 68 | + |
| 69 | +```ts |
| 70 | +app.put('/v1/users/:id', async (req, res) => { |
| 71 | + const user = await replaceUser(req.params.id, req.body) |
| 72 | + return res.status(200).json(user) |
| 73 | +}) |
| 74 | + |
| 75 | +app.patch('/v1/users/:id', async (req, res) => { |
| 76 | + const user = await updateUserPartial(req.params.id, req.body) |
| 77 | + return res.status(200).json(user) |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +**Bad snippet (semantic mismatch):** |
| 82 | + |
| 83 | +```ts |
| 84 | +app.get('/v1/logout', async (_req, res) => { |
| 85 | + await revokeSession() |
| 86 | + return res.status(200).json({ ok: true }) |
| 87 | +}) |
| 88 | +``` |
| 89 | + |
| 90 | +## Important Points |
| 91 | + |
| 92 | +- Use method semantics as interface guarantees |
| 93 | +- Do not hide mutations behind `GET` |
| 94 | +- Idempotency helps retries under network failure |
| 95 | +- `PATCH` is partial update, `PUT` is full replacement intent |
| 96 | + |
| 97 | +## Common Mistakes |
| 98 | + |
| 99 | +- Using `POST` for every endpoint because it feels easier |
| 100 | +- Treating `PATCH` as full replace and silently dropping omitted fields |
| 101 | +- Returning different behavior for the same method on similar resources |
| 102 | + |
| 103 | +## Summary |
| 104 | + |
| 105 | +- Method choice is behavior design, not naming preference. |
| 106 | +- Correct semantics reduce hidden production risk. |
| 107 | +- _If clients must guess behavior, semantics are already broken._ |
0 commit comments