Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST - Shouldn't PUT = Create and POST = Update

Tags:

rest

post

put

Shouldn't PUT be used to Create and POST used to Update since PUT is idempotent.

That way multiple PUTs for the same Order will place only one Order?

like image 244
Tawani Avatar asked Jun 04 '12 17:06

Tawani


People also ask

Can you put create and update?

PUT is used to both create and update the state of a resource on the server.

Is it okay to use POST FOR REST API updates?

tl;dr we cannot use PUTThe action performed by the POST method might not result in a resource that can be identified by a URI. To stay RESTful I would explain the update functionality as an Deletion of the old element and then a Creation of the new one, which would be an acceptable functionality for POST I would say.

Can we use POST to create and update?

So both POST/PUT can be used for insert/update (both submit data). It's up to the dev how they want to use - some like to map CRUD to the methods - others just POST or PUT for everything depending on idempotence.

Should I use POST or PUT for update?

Use PUT when you want to modify a single resource which is already a part of resources collection. PUT overwrites the resource in its entirety. Use PATCH if request updates part of the resource. Use POST when you want to add a child resource under resources collection.


2 Answers

The difference is that a PUT is for a known resource, and therefor used for updating, as stated here in rfc2616.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource.

I do see where you are coming from based on the names themselves however.

I usually look at POST as it should be the URI that will handle the content of my request (in most cases the params as form values) and thus creating a new resource, and PUT as the URI which is the subject of my request (/users/1234), a resource which already exists.

I believe the nomenclature goes back a long ways, consider the early web. One might want to POST their message to a message board, and then PUT additional content into their message at a later date.

like image 200
JP Silvashy Avatar answered Oct 14 '22 14:10

JP Silvashy


There's no strict correspondence between HTTP methods and CRUD. This is a convention adopted by some frameworks, but it has nothing to do with REST constraints.

A PUT request asks the server to replace whatever is at the given URI with the enclosed representation, completely ignoring the current contents. A good analogy is the mv command in a shell. It creates the new file at the destination if it doesn't exist, or replaces whatever exists. In either case, it completely ignores whatever is in there. You can use this to create, but also to update something, as long as you're sending a complete representation.

POST asks the target resource to process the payload according to predefined rules, so it's the method to use for any operation that isn't already standardized by the HTTP protocol. This means a POST can do anything you want, as long as you're not duplicating functionality from other method -- for instance, using POST for retrieval when you should be using GET -- and you document it properly.

So, you can use both for create and update, depending on the exact circumstances, but with PUT you must have consistent semantics for everything in your API and you can't make partial updates, and with POST you can do anything you want, as long as you document how exactly it works.

like image 37
Pedro Werneck Avatar answered Oct 14 '22 14:10

Pedro Werneck