I am a software architect working in service hosting area. I am interested and specialized in SaaS, Cloud computing and Parallel processing. Ricky is a DZone MVB and is not an employee of DZone and has posted 88 posts at DZone. You can read more from them at their website. View Full User Profile

RESTFul Design Patterns

06.01.2009
| 7845 views |
  • submit to reddit

Here I will summarize a set of RESTful design practices that I have used quite successfully.

Object Patterns


If there are many objects of the same type, the object URL should contains the id of the object.

http://www.xyz.com/library/books/668102 
If this object is a singleton object of that type, the id is not needed.

http://www.xyz.com/library

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
Host: www.xyz.com
Accept: application/json

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
Host: www.xyz.com
Content-Type: application/xml
Content-Length: nnn

<book>
<title>Restful design</title>
<author>Ricky</author>
</book>

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
Host: www.xyz.com
Content-Type: application/xml
Content-Length: nnn

<book>
<title>Restful design</title>
<author>Ricky</author>
</book>
HTTP/1.1 301 Moved Permanently
Content-Type: application/xml
Location: /library/books/668102

<ref>http://www.xyz.com/library/books/668102</ref>
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
Host: www.xyz.com



POST /library/books/668102?action=buy HTTP/1.1
Host: www.xyz.com
Content-Type: application/xml; charset=utf-8
Content-Length: nnn

<user>
<id>ricky</id>
<addr>175, Westin St. CA 12345</addr>
</user>

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
Host: www.xyz.com


Container Patterns


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.
http://www.xyz.com/library/books
http://www.xyz.com/library/dvds
http://www.xyz.com/library/books/668102/chapters


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.
GET http://www.xyz.com/library/book?query=(author='ricky')


Reference Patterns

Long running transactions

Concurrent Updates (ETags)

From  http://horicky.blogspot.com/

Published at DZone with permission of Ricky Ho, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Jonathan Carlson replied on Tue, 2009/06/02 - 2:06pm

Thanks! I'd love to hear your patterns for relating resources. How do you get from a book to it's author, for example. - Jon

Kenneth Mark replied on Sat, 2009/06/06 - 6:14am

Nice summary

Florent Guillaume replied on Wed, 2009/06/24 - 4:01pm

You're propagating a myth about what REST is.

Having nicely designed URLs is good of course, but this has nothing to do with REST. If at any point your application knows about the structure of URLs, then it's not REST anymore. The point of REST is that you follow URLs that previous queries returned to you in some manner, but you don't know how they are formed and you don't care.

Which, again, shouldn't prevent you from having nicely parsable URLs, just don't call that REST.

Andy Leung replied on Fri, 2009/10/30 - 11:49am in response to: Florent Guillaume

... The point of REST is that you follow URLs that previous queries returned to you in some manner, but you don't know how they are formed and you don't care. ...
How does that work? I am not sure if I understand this correctly. Without knowing what's the format of the URL, as a recipient of the request, how do you know what's coming in?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.