I’ve been working a lot recently with Yahoo’s great Javascript toolkit, YUI and have used it to talk to some web services I’ve created for various apps done in the REST style.
REST is an architectural style that encourages the use of HTTP verbs to accomplish your tasks. Other styles of web services rely quite heavily upon HTTP’s POST variable. One popular style, SOAP is like this. SOAP uses POST for pretty much everything and every stage of the request is wrapped up inside XML. SOAP is very powerful and very useful, but for simple data structures, it’s a bit of overkill.
REST is all about issuing simple commands to simple data structures.
HTTP 1.1 provides lots of verbs that all carry different semantic meanings. When working with in the REST style there are four verbs that get a lot of use:
- POST
- GET
- PUT
- DELETE
These four verbs can be mapped to the following four operations, repsectively: Create, Retrieve, Update, and Delete (aka CRUD). You can do a lot with these four operations when you think about it.
For an example, I’ll can define a blog (or any simple content management system) as a collection of web services. The following table demonstrates the different parts of a blog that we can implement in a REST fashion.
| Representation | URI | Explanation of Representation |
|---|---|---|
| Homepage (aka Article List) | http://example.org/ | Retrieves the Homepage for the blog |
| Article | http://example.org/<id> | An article posted to the blog—our blog’s bread and butter. |
| Category | http://example.org/category/<name> | A collection of articles with a given category. |
| Category List | http://example.org/all-categories | A collection of all categories for this blog. |
| User | http://example.org/users/<id> | A user of this blog |
| Comment | http://example.org/comment/<id> | A comment made by a user of the blog. |
| User List | http://example.org/userList | A collection of Users |
Now that we’ve defined the seven categories of our system, let’s look at what actions we want to perform on each type:
| URI | HTTP Verb | Effect |
|---|---|---|
| http://example.org/ | GET | Retrieves the Homepage for the blog |
| POST | Creates a new article | |
| http://example.org/<id> | GET | Retrieves an article posted to the blog |
| PUT | Edits an article on the blog. | |
| DELETE | Removes the article. | |
| http://example.org/category/<name> | GET | Retrieves a list of article URIs falling into the category <name> |
| DELETE | Removes this category from the blog. | |
| http://example.org/all-categories | GET | Retrieves a list of category URIs |
| POST | Adds a category to the blog | |
| http://example.org/users/<id> | GET | Retrieves a user’s info page |
| PUT | Updates a user’s information. | |
| DELETE | Removes this user from the blog | |
| http://example.org/comment/<id> | GET | Retrieves a comment. |
| PUT | Edits this comment. | |
| DELETE | Removes this comment from the blog. | |
| http://example.org/userList | GET | Retrieves a list of user URIs |
| POST | Creates a new user |
Tomorrow I’ll look at setting up our server environment to actually allow us to handle these requests.