What is the difference between PUT and PATCH?
Options
- PUT creates a resource; PATCH deletes a resource
- PUT replaces the whole resource; PATCH updates part of it (Correct option)
- PATCH is idempotent by definition, but PUT is not
- There is no difference, both update in the same way
Answer
PUT replaces the whole resource; PATCH applies a partial update
Why
PUT sends the COMPLETE new version of a resource and replaces the old one. Fields you leave out may be reset.
PATCH sends only the fields that change, e.g. {"email": "new@x.com"}.
PUT is idempotent; PATCH is not guaranteed to be (e.g. 'add 1 to counter').
Remember
PUT = replace all. PATCH = change some fields.