Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct HTTP method for RESTful create-or-update?

People also ask

Which of the HTTP methods can be used to update in rest?

PATCH is another HTTP method used to update resources. As opposed to replacing resources, like the PUT method does, PATCH only modifies resource contents.

Which HTTP method should be used to create update resource using RESTful web service?

The four major HTTP methods define the four operations that are commonly implemented by RESTful Services. The HTTP POST method is used for creating a resource, GET is used to query it, PUT is used to change it, and DELETE is used to destroy it.

What are the HTTP methods that can be used in RESTful web services?

HTTP methods GET − Provides a read only access to a resource. POST − Used to create a new resource. DELETE − Used to remove a resource. PUT − Used to update a existing resource or create a new resource.


The HTTP 1.1 specification says for POST:

9.5 POST

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

And for PUT:

9.6 PUT

The PUT method requests that the enclosed entity be stored under the supplied Request-URI.

Given that, and the fact that PUT is idempotent and POST is not, PUT seems the logical choice here for both your create and update.

Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2


Here is my salt test for using POST vs PUT:

The general idea is POST is used for creating, and PUT is used for updating. But in the create-or-update scenario, they both apply. The key here is you already know the resource's ID.

So...

If the URL contains the document's identifier, use PUT, otherwise use POST.

In your case, the identifier is known, and in the URL, so use PUT.