PUT vs PATCH vs POST
Published: 2023-10-13 02:54:45

The HyperText Transfer Protocol (HTTP) is the communications protocol that allows the client to interact with the server. An HTTP request is a request made from a client to a host located on the server in order to receive a resource needed to build the content. Three of the most commonly used HTTP requests are PUT, PATCH, and POST requests. PUT and PATCH requests are both common methods for updating or modifying resources on the server, while POST requests are used to either create a new resource or add a new entity to an existing resource. While they all serve similar purposes, they have distinct differences that are important for web developers to understand.

<u>PUT HTTP Request</u>

The PUT request is a method of modifying resources in which the client sends data that updates the entire resource and replaces it with a new representation. If the resource does not already exist, then the method creates a new resource. In the body of a PUT request, all of the fields of the resource are sent, even if they are not modified. 

Characteristics of a PUT request
- Replaces the Entire Resource: As already mentioned, when a client sends a PUT request, it sends a complete representation of a resource to the server. If parameters of the resource are missing in the body of the request, several things can happen depending on how the API is implemented. Some examples of possible outcomes are incomplete/ partial updates, validation errors/ error responses, the using of default values, or undefined behavior
- Not suitable for partial updates: Due to some of the errors listed above, PUT is not the best method for making partial updates. If you want to change only a single attribute of a resource without affecting the others, PUT is not the method that should be used
- URI: A put request creates or replaces a resource at the client defined URI. The client needs to know the exact URI of the resource, and the server must not attempt to apply the request to a different resource
- Idempotent: Put is an idempotent method. A request method is considered "idempotent" if the intended effect on the server of multiple identical requests with that method is the same as the effect for a single such request. This is helpful in scenarios where you want to ensure that a request can be safely retried without creating unintended side effects.

<u>PATCH HTTP Request
</u>
Unlike the PUT request, the PATCH request is a method that was designed to make partial updates to a resource. When the client sends a PATCH request to the server, it sends along a set of instructions that outline how the resource should be updated. PATCH requests do not require the client to send a representation of the entire resource like a PUT request does. This means that in the body of the PATCH request, only the fields that need to be updated are addressed. 

Characteristics of a PATCH Request
- Partial Updates: A PATCH request is the method you should use in a scenario where you want to modify certain attributes of a resource while leaving the rest intact. This can be useful when updating a large resource, in which sending the entire representation using PUT could be inefficient. 
- URI: A PATCH request updates parts of the resource at the client defined URI. The client needs to know the exact URI of the resource, and the server must not attempt to apply the request to a different resource
- Flexibility: PATCH allows you to update a resource using any format you want, including XML, JSON, or plain text
- Idempotent: A PATCH request is not necessarily idempotent, but it can be. This is different from a PUT request, which is always idempotent

<u>POST HTTP Request
</u>
The POST request is a method that is used to submit an entity to a resource by either creating a new resource or adding a new entity to an existing resource. The method sends the content that is passed to the body of the request message to the server, often with the intent of storing that content in the server. This request method is often sent via an HTML form and results in a change on the server. 

Characteristics of a POST Request
- URI: The POST request creates child resources at a server defined URI. When using POST for resource creation the server can decide the URI (and usually the ID) of the newly created resources
- Not idempotent: A POST request is not idempotent, which means that making a POST request more than one time may have additional side effects. For example, sending the same POST request multiple times could result in different outcomes or create multiple instances of the submitted data

<u>Differences</u>

PUT vs POST
- While both can be used to create a new resource, POST is utilized to create a resource where the server generates the URI. PUT updates or creates a resource at a specific URI that is decided by the client
- PUT is idempotent, while POST is not, making it safer to send the PUT request multiple times

PATCH vs POST
- POST creates a new resource while PATCH only modifies an existing one
- POST doesn’t require a specific URI while PATCH usually updates a resource at a known URI
- PATCH can be idempotent while PUT is not 

PUT vs PATCH
- PUT replaces the entire resource while PATCH makes partial modifications. PATCH does not require the client to send a representation of the entire resource, but PUT does
- PUT is always idempotent. PATCH has the ability to be idempotent, but is not always idempotent

In conclusion, understanding the differences between PUT, PATCH, and POST HTTP requests is important for efficiently handling resources during web development. While these methods share some similarities, each serves distinct purposes in the creation and updating of a resource. Choosing the appropriate method is necessary for providing efficiency in a web application.