Guides
Last updated
October 24, 2024

Difference Between PUT and PATCH Request

Emma Jagger

Table of Contents:

Get your free
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

Do you want to know what PATCH and PUT requests are, their importance for REST APIs, and, most importantly, their differences? Then, you are in the right place. Virtually speaking, of course.

Back to the Old Drawing Board

Once upon a time, there was something formally known as REST API. Similar to other APIs, REST was used to exchange data between different systems, and to interact with them, in a swift, efficient way. For example, a phone app and a server, an e-commerce platform and a payment gateway, or a CRM system and a marketing platform.

Yet, since “once upon a time” it’s rather “now upon a time”, let’s switch back to the present tense, for the sake of clarity. Our goal here is to state that, to achieve the data exchange or interaction, a request has to be made to the API server. To achieve it, REST API uses HTTP methods such as:

  • GET: It is used to read information from a record in the database.  
  • PUT: The primary function of this operation is to change an entire record's information in the database. It can also be used to create a new one.
  • POST: This method creates a new record in the database, though it’s not the same as PUT, as we will see. 
  • PATCH: It’s an operation used to update an existing resource, but does not require sending the entire body with the request.
  • DELETE: This operation removes a record from the database.  

A REST API request will also include the URL of the resource you want to interact with and headers that specify the request's content type and, if necessary, authorize the procedure. In PUT and POST requests, it will also include a body of data you want to send to the server. 

The successful response, in both scenarios, should be HTTP response code “200 OK” or “201 Created”. Otherwise, it will be a “400 Bad Request”, “401 unauthorized”, “404 Not Found” or, even, a “500 Internal Server Error” code.

Keep in mind that, while these requests can be set manually, they are often a key piece in automatic processes. That means that they will be generated without anyone writing them at the very moment. 

They can be triggered by a user performing certain actions, such as clicking on a button or writing something in a collaborative doc. Another option is to have them pre-seated for specific dates or events

The names of the HTTP method, as you can read, are sort of intuitive. Nonetheless, they require some clarification. Especially, when their functions seem to overlay, as it happens with PUT and PATCH, as well as with PUT and POST.

It is important to know the difference between PUT-PATCH (and, probably POST), almost by heart. This will help you to use your API’s resources more efficiently, as well as avoid duplicates, and performance issues, and improve the user experience.

That being said, let’s get down to business.

Let’s send your first free
call
See why the best developers build on Abstract
Get your free api

What is a PUT request?

A PUT request is a set of instructions sent to the API, to update a record’s information. Opposite to PATCH, PUT alters the entire data of a resource. That means, that whatever figures in the body of the request will be the resource after the request has been successfully implemented. 

PUT can also be used to create new resources. The content of the request is essentially the same as when modifying existing ones. But, since the soon-to-be resource will not have already a URL, you’ll have to define one when writing the request.

Idempotency: PUT vs. POST 

One of the definitory traits of PUT requests is its idempotency. Technically speaking, idempotency is a mathematical concept. But, when it comes to PUT requests, it means that, regardless of how many times an action is performed, the results will be the same as the first time. 

Let’s take POST requests, for example. If you issue a POST request several times, for whatever reason, you will create multiple resources with the same content. On the contrary, if you issue several times a single PUT request, it will not have any effects on the specified resource, beyond the first time.  

In other words, a PUT request affects a single resource. If a PUT request is repeated, it will not alter that resource, since it already displays the request’s data. This ensures that the REST API will be reliable and perform consistently, avoiding duplicates, processing errors, and unwanted results.

H3: Script of a PUT

Let’s have an example of how a PUT request to modify or create a resource would look like:

fetch('https://api.example.com/usuarios/123', {

    method: 'PUT',

    headers: {

        'Content-Type': 'application/json',

        'Authorization': 'Bearer your-access-token'

    },

    body: JSON.stringify({

        name: "John Smith",

        email: "john.smith@example.com"

        age: “30”

    })

})

In this case, you seek to change or create a user’s profile (user 123), assuming that all the profile fields are his name, email, and age. The “fetch” function will allow you to send the request to the API server, using JavaScript.

What is a PATCH request?

A PATCH request is a set of instructions to alter some pieces of data, that an API stores in a server. That is, it only updates a few sections of existing resources, or “child resources”. 

Think about it changing an entire outfit (PUT request) vs. changing only a shirt (PATCH request). While the first one alters your entire appearance, the second one modifies only a specific part of it. 

Its content is roughly the same as in a PUT request, but the “body” section only displays the data that are to be modified. Therefore, the rest of the script’s content should remain unchanged once the request is executed.

By sending fewer data and not updating an entire resource, PATCH proves to be a more efficient method than PUT.  At least, when specific changes are required. 

Unlike PUT, PATCH is not idempotent, which means that sending multiple times a single PATCH request might yield different results. That is because PATCH alters a resource based on its current state… And, after the first time a PATCH request is sent, the resource at issue will be already modified. 

This, as well as the fact that not all servers accept PATCH, often causes PUT to be more commonly used than PATCH

Script of a PATCH

A PATCH request for changing only the age of a user (the same user as the PUT example) would look like this:

fetch('https://api.example.com/usuarios/123', {

    method: 'PATCH',

    headers: {

        'Content-Type': 'application/json',

        'Authorization': 'Bearer your-access-token'

    },

    body: JSON.stringify({

       age: “31”

    })

})

As you can see, the field “age” displays the content that has to be updated as it should be after it is modified. The method name has also been changed, from PUT to PATCH.

PUTCH vs. PATCH

In brief, the main distinctions between PATCH and PUT are the following ones:

  • PUT updates an entire resource. PATCH, only the sections of it which are specified.
  • PATCH is more efficient than PUT.
  • PUT is idempotent, while PATCH is not.
  • PATCH is not accepted by every server. PUT, normally, yes.

When to use PATCH or PUT?

Now that you know the differences between PUT and PATCH, it is time to delve into when each one of those methods should be used, to ensure your API works properly. 

PUT requests are to be issued in the following situations:

  • When fully replacing a resource’s content. The PUT request will have to provide the data for every field of the resource. When not, the field may be set in default mode or be eliminated.
  • Scenarios that requiere idempotency. That is when one has to make sure a request will always have the same result, notwithstanding how many times it is sent.
  • When creating a new resource. PUT can be preferred over POST as a means of creating new resources when the client knows the desired identifier of the resource beforehand. In other words, its location. It is also a better option than POST when idempotency is needed.

By contrast, PUT requests are recommended for:

  • Partial modifications. PATCH updates only certain fields of a specific resource, listed in the request’s script. This implies more efficient management of an API’s content, preventing unintended modifications and enhancing fluency in data exchange.
  • Frequent updates & scenarios that require efficiency. Since only the necessary content is sent and altered, fluency in updates, frequents or not, is assured.
  • Complex resources, with big data. Using PATCH helps to avoid accidentally deleting information since only the fields listed in the script will be modified. 

Can PATCH and PUT be used interchangeably?

Though it is not always recommended, PUT can be used as a PATCH request in very specific situations

The script example provided above is one of those specific situations. Why? Because it has a simple structure, with little data. Therefore, the speed of the request will not be substantially affected, and filling every field of the resource in the script will not pose any inconvenience.

In brief…

PATCH and PUT are key factors when updating an API’s resources. While PUT allows for a more “total” altering of its content, PATCH provides punctual modifications. Both of them “protect” the resource against unwanted changes, though each one has its own methods to do it. 

Choosing between PATCH or PUT depends mostly on the amount of data you plan to update and the complexity of the resources involved. Knowing when to use each one, helps you make sure that an API’s resources are used more efficiently and, of course, it improves its user’s experience.

Emma Jagger

Emma Jagger is an experienced engineer and Google alumna with a degree from Carnegie Mellon University. She specializes in email validation, IP geolocation, and API integration, focusing on creating practical and scalable solutions through her technical writing.

Get your free
key now
See why the best developers build on Abstract
get started for free

Related Articles

Get your free
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required