PUT vs PATCH: Understanding the Differences with Examples

Introduction

In the world of web development, particularly when working with RESTful APIs, the HTTP methods PUT and PATCH are often used to update resources. While they might seem similar at first glance, they serve different purposes and are used in distinct scenarios. This article will explore the differences between PUT and PATCH and provide examples to illustrate their usage.

PUT Method

PUT is used to update or replace an entire resource. When you use PUT, you're sending the complete representation of the resource to the server. If the resource doesn't exist, PUT can create a new one. However, if the resource already exists, it will be completely replaced by the new data.

Characteristics of PUT

  • Idempotent: Calling PUT multiple times will have the same effect as calling it once.
  • Complete Replacement: It replaces the entire resource with the new data provided.
  • Resource URI: PUT is typically used to update a specific resource identified by a URI (Uniform Resource Identifier).

Example

Consider a user resource with the following representation.

{
  "id": 1,
  "name": "John Doe",
  "email": "[email protected]"
}

Request

PUT /users/1 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "id": 1,
  "name": "Jane Smith",
  "email": "[email protected]"
}

Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 1,
  "name": "Jane Smith",
  "email": "[email protected]"
}

In this example, the PUT request replaces the entire user resource with the new data. If the name field were omitted in the request, it would be removed from the resource, and only the fields provided would be updated.

PATCH Method

PATCH is used to make partial updates to a resource. Unlike PUT, PATCH only updates the fields provided in the request. This is useful when you only need to change a specific part of a resource without affecting the rest.

Characteristics of PATCH

  • Not Necessarily Idempotent: While PATCH can be idempotent, it's not always guaranteed to be.
  • Partial Update: It updates only the specified fields of the resource.
  • Resource URI: PATCH is also used to update a specific resource identified by a URI.

Example

Consider the same user resource.

Request

PATCH /users/1 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "email": "[email protected]"
}

Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 1,
  "name": "John Doe",
  "email": "[email protected]"
}

In this example, the PATCH request updates only the email field of the user resource. The name field remains unchanged, and only the specified field is modified.

When to Use PUT vs PATCH?

  • Use PUT when you need to replace the entire resource or when you are creating a new resource at a specific URI.
  • Use PATCH when you need to make partial updates to a resource without affecting other fields.

Summary

  • PUT: Replaces the entire resource or creates it if it doesn’t exist.
  • PATCH: Makes partial updates to the resource.

Conclusion

Understanding when to use PUT versus PATCH can help you design more efficient and predictable APIs. While PUT is suitable for complete resource updates, PATCH is ideal for making incremental changes to a resource.


Similar Articles