which convention is "correct"

I'm well into implementing a REST service (on a Windows CE platform if that matters) and I started out using IBM's general definitions of using POST for creating (INSERTs) and PUT for updating.

Now I've run across Sun's definitions which are exactly the opposite. So my question is, which is the "generally accepted" definition? Or is there even one?


The disadvantage of using PUT to create resources is that the client has to provide the unique ID that represents the object it is creating. While it usually possible for the client to generate this unique ID, most application designers prefer that their servers (usually through their databases) create this ID. In most cases we want our server to control the generation of resource IDs. So what do we do? We can switch to using POST instead of PUT.

So: Put = UPDATE

Post = INSERT


The reason to use POST as INSERT and PUT as UPDATE is that POST is the only nonidempotent and unsafe operation according to HTTP. Idempotent means that no matter how many times you apply the operation, the result is always the same. In SQL INSERT is the only nonidempotent operation so it should be mapped onto POST. UPDATE is idempotent so it can be mapped on PUT. It means that the same PUT/UPDATE operation may be applied more than one time but only first will change state of our system/database.

Thus using PUT for INSERT will broke HTTP semantic viz requirement that PUT operation must be idempotent.


The verbs are:

GET {path} : Retrieve the resource whose identifier is {path} .

PUT {path} : Create or update the resource whose identifier is {path} .

DELETE {path} : Delete the resource whose identifier is {path} .

POST {path} : Invoke an action which is identified by {path} .

When the intention is to create a new resource, we can use PUT if we know what the identifier of the resource should be, and we can use POST if we want the server to determine the identifier of the new resource.

链接地址: http://www.djcxy.com/p/45346.html

上一篇: 在REST中需要更多动词时该怎么做

下一篇: 哪个惯例是“正确的”