RESTFul Design Patterns
Here I will summarize a set of RESTful design practices that I have used quite successfully.
If there are many objects of the same type, the object URL should contains the id of the object.
If this object is a singleton object of that type, the id is not needed.http://www.xyz.com/library/books/668102
Get the object representation
HTTP GET is used to obtain a representation of the object, which has optional extension convention to indicate which format is needed. HTTP header "Accept" is also used to indicate the expected content format. Note also that the representation of the whole object is returned. There is no URL representation at the attribute level.
GET /library/books/668102.json HTTP/1.1
Modify an existing ObjectHTTP PUT is used to modify the object, the request body contains the representation of the Object after successful modification.
Create a new Object
HTTP PUT is also used to create the object if the caller has complete control of assigning the object id, the request body contains the representation of the Object after successful creation.
PUT /library/books/668102 HTTP/1.1
If the caller has no control in the object id, HTTP POST is made to the object's parent container with the request body contains the representation of the Object. The response body should contain a reference to the URL of the created object.
POST /library/books HTTP/1.1
HTTP/1.1 301 Moved Permanently
Call a method of the Object
HTTP POST is used to invoke a method of the object, the method is indicated in a mandated parameter "action". The arguments of the method can also be encoded in the URL (for primitive types) or in the request body (for complex types)
POST /library/books/668102?action=buy&user=ricky HTTP/1.1
POST /library/books/668102?action=buy HTTP/1.1
Content-Type: application/xml; charset=utf-8
<addr>175, Westin St. CA 12345</addr>
Destroy an existing ObjectHTTP DELETE is used to destroy the object. This release all the resources associated with this object.
DELETE /library/books/668102 HTTP/1.1
The immediate parent of a container must be an object (can be a singleton object without an id or an object with an id). Container "contains" other objects or containers. If a container is destroyed, everything underneath will be destroyed automatically in a recursive manner.
In GET operation, by default the container only return the URL reference of its immediate children. An optional parameter "expand" can be used to request the actual representation of all children and descendants.
A more sophisticated GET operation can contain a "criteria" parameter to show only the children that fulfills certain criteria.
Long running transactions
Concurrent Updates (ETags)