Should an edit of a comment be sent through POST or PUT?
I have the following URI: Posts/{postId}/Comments/{commentId}
From what I have read (in RESTful Web Services, published by O'Reilly), it seems clear that you should use PUT
to update an existing comment.
PUT
is meant to be used for updating as well as creating a resource.POST
can also be used for creating a resource. The difference here is that whenPOST
ing, you don't need to know the exact URI of the resource to be created. (The service will report the new resource's URI in its response.)POST
is appropriate for partial updates, or when appending information to a resource;PUT
is appropriate for a full update (replacement) of a resource.When updating, you can send partial updates, but you should make sure that these are idempotent; ie. if you send the same update more than once, the update will always have the same effect. Don't send an update such as "Increase n by 1"; instead, send an update such as "Set n to 5."
Thus, my suggestion for your case are as follows:
Use
POST
to/Posts/{postId}/Comments
to create a new comment, since the client doesn't know the{commentId}
in advance.Use
PUT
/Posts/{postId}/Comments/{commentId}
to completely update a comment (or perhapsPOST
when appending text to it).
see here: PUT vs POST in REST
精彩评论