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?
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?
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.
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.
PUT should be used for creates if and only if possible URI of the new resource is known for a client. New URI maybe advertised by the service in resource representation. For example service may provide with some kind of submit form and specify action URI on it which can be a pre populated URI of the new resource. In this case yes, if initial PUT request successfully creates resource following PUT request will only replace it.
It's ok to use POST for updates, it was never said that POST is for "create" operations only.
You are trying to correlate CRUD to HTTP, and that doesn't work. The philosophy of HTTP is different, and does not natively correspond to CRUD. The confusion arises because of REST; which does correspond to CRUD. REST uses HTTP, but with additional constraints upon what is allowed. I've prepared this Q & A to explain the HTTP approach to things:
What's being requested?
POST
requests an action upon a collection.PUT
requests the placement of a resource into a collection.What kind of object is named in the URI?
POST
identifies a collection.PUT
identifies a resource (within a collection).How is the object specified in the URI, for POST
and PUT
respectively?
/collectionId
/collectionId/resourceId
How much freedom does the HTTP protocol grant the collection?
POST
, the collection is in control.PUT
, the requestor is in control (unless request fails).What guarantees does the HTTP protocol make?
POST
, the HTTP protocol does not define what is supposed to happen with the collection; the rfc states that the server should "process ... the request according to the [collection's] own specific semantics." (FYI: The rfc uses the confusing phrase "target resource" to mean "collection".) It is up to the server to decide upon a contract that defines what a POST
will do.PUT
, the HTTP protocol requires that a response of "success" must guarantee that the collection now contains a resource with the ID and content specified by the request.Can the operation result in the creation of a new resource within the collection?
POST
creates a new resource, the response will be 201.PUT
will generally not insert, but only update.) When a PUT
creates a new resource, the response will be 201.Is the operation idempotent?
POST
is generally not idempotent. (The server can offer any contract it wishes, but idempotency is generally not part of that contract).PUT
is required to be idempotent. (The state of the identified resource is idempotent. Side effects outside of that resource are allowed.)Here is the rfc: https://www.rfc-editor.org/rfc/rfc7231#section-4.3.3
It depends.. you can create/update sites/records with both. When the client is specifying the URI then PUT is the way to go. e.g. Any Code Editor like Dreamweaver, PUT is the right protocol to use.
have also a look at this thread: put vs post in rest