REST API
HTTP Methods
Web Development
PUT vs PATCH
API Best Practices

Use of PUT vs PATCH methods in REST API real life scenarios

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

REST APIs have become the backbone of many web services. Understanding the nuances of HTTP methods is critical for developers to design robust, efficient, and maintainable APIs. Two such methods, PUT and PATCH, are widely used for updating resources but serve different purposes and have distinct implications on API design and performance.

Understanding PUT and PATCH Methods

PUT Method

The PUT method is idempotent, meaning that multiple identical requests should have the same effect as a single request. When a PUT request is made, it updates the target resource with the full new version provided in the request body. If a field in the resource is not specified in the PUT request body, it is assumed that the field is not applicable and should be removed or set to default.

Example Usage of PUT:

Suppose you have a User resource with fields name, email, and age. A PUT request to update this user might look something like:

http
1PUT /users/123 HTTP/1.1
2Host: example.com
3Content-Type: application/json
4
5{
6    "name": "John Doe",
7    "email": "[email protected]",
8    "age": 30
9}

If the original user resource includes a phoneNumber, and the PUT request does not have this field, phoneNumber would typically be removed or reset.

PATCH Method

In contrast, the PATCH method is used for making partial updates to a resource, meaning it only requires the fields that need to be updated. PATCH is neither safe nor idempotent, which means subsequent identical requests may have different effects.

Example Usage of PATCH:

Continuing with the User example, if only the email needs to be updated, a PATCH request might look like:

http
1PATCH /users/123 HTTP/1.1
2Host: example.com
3Content-Type: application/json
4
5{
6    "email": "[email protected]"
7}

Here, only the email field of the User resource would be changed; other fields like name and age would remain unaffected.

Comparing PUT and PATCH for API Design

Scenario Analysis

  • Complete Update vs. Partial Update: Use PUT when you have a complete new version of the resource. Use PATCH when modifying only a subset of the resource's data. This reduces the amount of data sent over the network and can optimize backend processing.
  • Idempotence Concerns: Because PUT is idempotent, it's a safer choice for operations that must maintain this property. For example, in situations where a network request might be automatically retried in case of failure.
  • Handling Large Resources: When dealing with large resource representations, PATCH can be more efficient and network-friendly as it only sends the changes rather than the complete resource.

Use Case Examples

  • User Profiles: In a system where users can update their profiles, using PATCH for updates means they can send only the data they are changing (like a new profile picture or a phone number) instead of resending the entire user profile.
  • Configuration Systems: In systems where configuration settings are complex and comprise many fields, a PUT method would require sending all configuration fields even if only one field has changed, whereas PATCH would send only the changed field.

Summary Table

FeaturePUTPATCH
Full UpdateYes (required)No (not required)
Partial UpdateNot applicableYes (only changes)
IdempotenceYesNo
EfficiencyLower for large resources/many fieldsHigher for large resources/few fields
Network-friendlyLess (transfers complete resource)More (transfers only modifications)

Conclusion

Choosing between PUT and PATCH methods in API design largely depends on your specific application requirements. For large, frequently modified resources, PATCH can offer efficiencies both in terms of bandwidth and processing time. For scenarios requiring guaranteed idempotence, PUT is the preferred choice. By understanding the technical and practical distinctions between these two methods, developers can design more effective and appropriate APIs.


Course illustration
Course illustration

All Rights Reserved.