Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I update a REST resource?

Tags:

rest

I'm not sure how I should go about updating individual properties of a REST resource. Consider the following example:

# HTTP GET to /users/1.xml
<?xml version="1.0" encoding="UTF-8" ?>
<response>
   <user>
      <id>1</id>
      <name>John Doe</name>
      <email>[email protected]</email>
   </user>
</response>

How should I facilitate for updating John's email? HTTP PUT comes to mind, but I'd be making it hard on my clients by requiring a complete XML (matching the HTTP GET response) to modify the resource.

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server.

Is there any other way?

like image 279
Johannes Gorset Avatar asked Jun 30 '10 14:06

Johannes Gorset


People also ask

What is the appropriate rest method to update a resource on the server?

As a RESTful API HTTP method, PUT is the most common way to update resource information.

How do I update data on REST API?

To use the REST API to update existing model objects, complete one of the following two methods: Use the model object resource representing the existing object and the HTTP PUT method, passing the new object data in the body of the request.

Why should you use put over POST to update a resource in REST?

Another important difference between the methods is that PUT is an idempotent method, while POST isn't. For instance, calling the PUT method multiple times will either create or update the same resource. In contrast, multiple POST requests will lead to the creation of the same resource multiple times.

Which rest method is used to update a resource partially?

The HTTP PATCH request method applies partial modifications to a resource. PATCH is somewhat analogous to the "update" concept found in CRUD (in general, HTTP is different than CRUD, and the two should not be confused).


1 Answers

If your server framework is flexible enough to handle it, you can do:

Request:
PUT /users/1/email
Content-Type: text/plain

[email protected]

Response:
200 OK
Content-Location: /users/1

By using a URL to refer to the email as its own resource, you can PUT directly to it using a simple format like text/plain. In the response, the Content-Location url gives the client an indication that the change has had an impact on the user resource.

The PATCH method is also another way that you can do partial updates. This is a newly introduced method and as yet there are no standard formats for sending XML diff documents. So, if you take this approach you will not find much guidance.

The other thing to consider is that REST works best with large grained updates. If you find yourself needing to make these kinds of small changes, then maybe you need to rethink your distributed architecture.

like image 98
Darrel Miller Avatar answered Oct 01 '22 05:10

Darrel Miller