What is the difference between PUT and PATCH, and how do you implement PATCH?
Quick Answer
PUT replaces the whole resource — you send the complete representation, and it's idempotent. PATCH applies a partial update — you send only the changes. Two formats: JSON Merge Patch (send a partial object) and JSON Patch (RFC 6902 — an array of operations like replace /name).
PUT means “here is the entire new state of this resource” — you send everything and it overwrites, which makes it idempotent. PATCH is a partial update — you send just what changed. There are two flavours: merge patch, where you send a partial JSON object, and JSON Patch, which is a list of operations like “replace the name field.” In ASP.NET Core I take a JsonPatchDocument<T>, apply it to a DTO, and then validate. One thing to note is PATCH isn't automatically idempotent — an add operation appends every time — so I design the operations carefully.
- ASP.NET Core:
[HttpPatch] public IActionResult Patch(int id, [FromBody] JsonPatchDocument<CustomerDto> patch)— apply withpatch.ApplyTo(dto, ModelState), then validate. - JSON Patch needs
AddNewtonsoftJson()in current ASP.NET Core. - Merge patch is simpler for clients but can't express “set this field to null” unambiguously.
- PATCH isn't guaranteed idempotent (an
addop appends each time); design your ops to be idempotent if you want safe retries.