In CouchDB, to create a new document you send:
PUT /albums/70b50bfa0a4b3aed1f8aff9e92dc16a0
Isn't PUT used to update data and not creating it?
PUT is used to both create and update the state of a resource on the server.
which http verb/best practice to update an entity without payload to send? The general rule is that you use POST unless the semantics of your message match a more specific method. PUT with an empty payload is a request to make the current representation of the resource zero bytes long.
Generally, in practice, always use PUT for UPDATE operations. Always use POST for CREATE operations.
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.
It's used for both. Quoth the RFC:
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. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.
The key term for PUT for me is always idempotent. While for POST you are always "adding another" item to the systems-state, with PUT the action is the same even if multiple times performed (because you are adressing an item).
Example:
doing 100-times POST /albums = you would end up with 100 different albums (but with same content)
doing 100-times PUT /albums/123 = you would end up with one single album with id 123 (with the content)
PUT is to create a new or replace entirely an existing resource when you know the existing URI or what the new URI will be. POST is for updating parts of an existing resource, or for creating a new resource when the server has to assign the new URI. It's that simple. Both PUT and POST are used for creates and updates, it's not about if you're creating or updating, it's about whether you already know the URI or you need the server to assign it for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With