ASP.NET Core Web API Interview Question #11
What is the difference between PUT and PATCH?
Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner
Quick Interview Answer
PUT generally represents replacement or a complete update of the target resource representation, while PATCH represents a partial modification. The exact contract must be documented because APIs sometimes use PUT with update DTOs that do not literally contain every stored field.
Detailed Explanation
PUT generally represents replacement or a complete update of the target resource representation, while PATCH represents a partial modification. The exact contract must be documented because APIs sometimes use PUT with update DTOs that do not literally contain every stored field.
PATCH can reduce payload size but requires careful validation of which fields may be modified. Neither method should allow clients to change protected server-managed fields simply because they appear in the request.
Code Example
[HttpPut("{id:int}")]
public IActionResult Update(
int id,
UpdateProductRequest request) => NoContent();
[HttpPatch("{id:int}")]
public IActionResult Patch(
int id,
JsonPatchDocument<ProductDto> patch) => NoContent();