What is the RESTful way to add an untitled item to a set?
Say I want to write an interface exposing the set of books a user has read. Since I want URIs to be human-readable, they'll be of the form /books/War-and-Peace
. Adding a book to the set seems like a PUT operation, as it 1) creates a new resource, and 2) is idempotent, since a set cannot have duplicate elements. However, the PUT method requires the client to know the URI of the resource to be cr开发者_运维技巧eated, which must be calculated on the server in this case, since there may be multiple books with the same title, which would be represented as /books/War-and-Peace
and /books/War-and-Peace-2
. A hacky solution, of course, is to use POST, which is allowed to create new resources, but is not guaranteed to be idempotent.
My solution is the following:
- The client PUTs to
/book-hashes/<hash>
, where<hash>
is a hash of the book object. - The server creates a reference to the object at both that URI and
/books/<title>
. - The server issues a 303 See Other status code pointing to
/books/<hash>
. - The client GETs
/book-hashes/<hash>
. - The server responds with a 301 Moved Permanently, pointing to
/books/<title>
. - The client GETs
/books/<title>
and stores it as the new URI for the book resource. - The server deletes
/book-hashes/<hash>
.
Is this a legitimate REST solution? Has anyone solved the problem a different way?
There's nothing wrong with using POST here. POST is commonly used to "add" things and then return the URI of the created resource.
So
POST /books?title=War-and-Peace
303
Location: /books/War-and-Peace-2
How about?
GET /BookSlug?title=War%20and%20Peace
=>
200 OK
Content-Type: text/plain
war-and-peace
followed by
PUT /Book/war-and-peace
<BookContent>
=>
201 Created
精彩评论