Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't HTTP verb PUT used for updating and not creating content?

Tags:

rest

http

couchdb

In CouchDB, to create a new document you send:

PUT /albums/70b50bfa0a4b3aed1f8aff9e92dc16a0

Isn't PUT used to update data and not creating it?

like image 967
never_had_a_name Avatar asked Oct 14 '10 17:10

never_had_a_name


People also ask

Is Put used for update or create?

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

Which HTTP verb or verbs can be used to update an entity?

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.

Should I use POST or PUT for update?

Generally, in practice, always use PUT for UPDATE operations. Always use POST for CREATE operations.

Is Put the same as update?

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.


3 Answers

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.

like image 177
Victor Nicollet Avatar answered Sep 30 '22 16:09

Victor Nicollet


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)

like image 39
manuel aldana Avatar answered Sep 30 '22 16:09

manuel aldana


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.

like image 37
Ramon Leon Avatar answered Sep 30 '22 15:09

Ramon Leon