All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is the difference between PUT and PATCH?

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
PUT and PATCH are two HTTP methods used to update resources on the server in the context of RESTful web services. The main difference between them lies in how they interact with existing resources.

PUT (Object Replacement)

The PUT request indicates that the client is sending a new representation of the resource that should completely replace the current state of the resource or create a new one if it does not exist. In other words, the entire resource must be replaced with the new one.
Ruby on Rails example:
# Controller
def update
  @user = User.find(params[:id])
  if @user.update(user_params)
    render json: @user
  else
    render json: @user.errors, status: :unprocessable_entity
  end
end
In this example, update is used with a PUT request, and @user.update(user_params) replaces all user attributes with new values coming from the client. It implies that user_params contains all parameters of the object (model).

PATCH (Partial Update)

The PATCH request indicates that the client is sending a partial update of the resource, meaning only the data that needs to be changed, without the need to send all the information about the resource. This is convenient when the client wants to update only certain fields of the resource. For example - only one section of the form with information about themselves on the site.
Ruby on Rails example:
# Controller
def update
  @user = User.find(params[:id])
  if @user.update(user_partial_params)
    render json: @user
  else
    render json: @user.errors, status: :unprocessable_entity
  end
end
Here @user.update(user_partial_params) is used for partial user update with a PATCH request, where user_partial_params contains only those fields that need to be updated.
In summary, PUT is used for complete resource updates, while PATCH is used for partial updates, allowing other parts of the resource to remain unchanged.
Ruby on Rails is quite an intelligent framework and it determines itself which request it needs to make. This allows for automation and simplifies controller logic, making the code cleaner and more understandable.
So technically, the call @user.update(user_params) (which we considered earlier as PUT) can be PATCH instead of PUT, because the form on the frontend does not have all the fields of the object.

This post doesn't have any additions from the author yet.

19 Dec 18:15

What are SSL and TLS? What is the difference?

meme code
meme code@memecode
19 Dec 18:17

What is the HTTP protocol? What is the difference between HTTP and HTTPS?

meme code
meme code@memecode
19 Dec 19:10

What is a "man-in-the-middle" attack?

meme code
meme code@memecode
20 Dec 17:33

What is an API? Can a web application use more than one API?

meme code
meme code@memecode
23 Dec 10:18

What is a REST API? The basic principles of REST and GET, POST, PUT, PATCH, DELETE.

meme code
meme code@memecode
23 Dec 10:22

What is RESTful and how does it differ from REST?

meme code
meme code@memecode
23 Dec 11:29

What are cookies?

meme code
meme code@memecode
23 Dec 11:40

What is a session? (Web)

meme code
meme code@memecode
24 Dec 12:08

What is penetration testing?

meme code
meme code@memecode
24 Dec 12:08

What is SQL Injection?

meme code
meme code@memecode
What is DOM?
24 Dec 12:22

What is DOM?

meme code
meme code@memecode
24 Dec 12:23

What is Cross-Site Scripting (XSS)?

meme code
meme code@memecode