HTTP Methods in APIs
GET (read)
- Safe (should not change server state) and commonly cacheable when response headers allow.
- Must not carry a body with semantics in many stacks; use query parameters for filters.
- Use 200 with a body, 204 for no content, or 404 when the resource is missing (be consistent across the API).
POST (create)
- Used to create resources or trigger non-idempotent processing (side effects, workflows).
- Often returns 201 Created with a
Location header pointing to the new resource when applicable.
- Not inherently idempotent: retries can duplicate unless you add idempotency keys or de-duplication.
PUT vs PATCH (update differences)
- PUT: replace the entire resource representation the client sends; idempotent when the server interprets it as “set state to this.”
- PATCH: partial update (JSON Merge Patch, JSON Patch, or custom schema); define semantics clearly to avoid ambiguous merges.
- If clients cannot send full objects, PATCH (or POST to a sub-resource) is usually clearer than overloading PUT.
DELETE (remove)
- Should remove or soft-delete consistently; document whether deletes are recoverable.
- Often idempotent: deleting twice yields the same end state (204 or 404 after gone).
- Consider async deletion for heavy resources (202 + job id) when work cannot finish inline.
Idempotency concept
- Idempotent methods can be repeated safely: same outcome (GET, PUT with stable ids, DELETE).
- POST is the usual place where idempotency matters most; use Idempotency-Key headers or natural keys.
- Document how long idempotency records are retained and what happens on conflict.