You are currently viewing Difference Between PUT and PATCH Methods?

Difference Between PUT and PATCH Methods?

Hello Spectaculars,

Welcome to this article of ServiceNow Spectaculars.

In this article, we discuss two important methods of RESTful Web Services. This question is viewed about 10k times in ServiceNow Community.

What is PUT method?

  • The PUT method is a standard HTTP method that is used to update an existing resource on a web server.
  • In RESTful web services, the PUT method is typically used to replace the entire representation of a resource with a new one, while preserving the resource’s unique identifier.
  • With PUT method the client sends a request to the server with the updated representation of the resource in the request body.
  • The server then replaces the existing representation of the resource with the new one provided in the request, based on the unique identifier of the resources.

Here’s an example of a PUT request in HTTP format:

PUT /api/products/1 HTTP/1.1
Host: example.com
Content-Type: application/json
{
    "name": "Product A",
    "price": 19.99,
    "description": "A high-quality product"
}

In this example, the client is updating an existing product resource with ID “1” by sending a PUT request to the server. The request contains the updated representation of the product in JSON format in the request body. The server updates the current representation of the product with the new one supplied in the request when it gets it.

Here PUT method is idempotent, means that if the same request is sent multiple times, the server always return the same result. This makes the PUT method suitable for cases where multiple requests may be sent for the same update.

What is PATCH method?

  • The PATCH method is a standard HTTP method that is used to update a portion of an existing resource on a web server.
  • The PATCH technique is commonly used in RESTful web services to alter one or more fields of a resource without changing the resource’s whole representation.
  • In PATCH method, client sends a request to the server with a set of instructions on how to modify the resource in the request body.
  • The server then applies the changes to the resource, based on the unique identifier of the resource.

Here’s an example of a PATCH request in HTTP format:

PATCH /api/products/1 HTTP/1.1
Host: example.com
Content-Type: application/json-patch+json
[
    { "op": "replace", "path": "/price", "value": 24.99 }
]

In this example, the client is updating an existing product resource with ID “1” by sending a PATCH request to the server. The request contains a set of instructions in the JSON Patch format in the request body. In this case, the instruction is to replace the value of the “price” field with a new value of 24.99.

When the server gets the request, it updates the value of the “price” field to 24.99 and updates the resource accordingly, keeping the other values (like “name” and “description”) intact.

Here the PATCH method is not idempotent, means that if the same request is sent multiple times, the server may return different results. This makes the PATCH method suitable for cases where multiple requests may be sent for different updates.

Difference between PUT & PATCH?

Below is the table summarizing the differences between PUT and PATCH methods in REST and SOAP web services:

FeaturePUT MethodPATCH Method
PurposeUpdates entire resourceUpdates part of a resource
IdempotentYes (multiple requests have the same effect as a single request)No (multiple requests may have different effects)
Request BodyRequires entire updated representation of the resourceRequires only the changes to the resource
Use CaseUpdating a resource that requires the entire representation to be providedUpdating a resource with only partial information
REST EquivalentHTTP PUT methodHTTP PATCH method
SOAP EquivalentUpdate methodUpdatePartial method

Overall, PUT and PATCH methods are both useful for updating resources in RESTful and SOAP web services, but they have different use cases and behaviors that make them suited for different types of updates.

Leave a Reply