Sobes.tech
Junior — Middle

Explain the differences between HTTP PUT and PATCH methods when updating resources.

sobes.tech AI

Answer from AI

Main differences between HTTP methods PUT and PATCH when updating resources:

  • PUT — used for full update of a resource. The client sends a complete representation of the resource, and the server replaces the existing resource with this new one.
  • PATCH — used for partial update of a resource. The client sends only the fields that need to be changed, and the server updates only those, leaving others unchanged.

Example:

If you have a user object with fields name and email:

  • With a PUT request, you must send both fields; missing ones will be deleted or reset.
  • With a PATCH request, you can send only email, and the server will update only that.

In C# Web API, methods might look like this:

[HttpPut("users/{id}")]
public IActionResult UpdateUser(int id, User user) {
    // Full update of user
}

[HttpPatch("users/{id}")]
public IActionResult PatchUser(int id, JsonPatchDocument<User> patchDoc) {
    // Partial update of user
}

Thus, PUT is an idempotent method for full replacement, PATCH is for partial modification.