Continuing our look at Representational State Transfer interfaces, part three lets us get our hands dirty with some real-life RESTful interfaces from BrightKite and del.icio.us.
What Is A RESTful Interface?
Three articles into the series and I finally get to explain this, thanks for sticking around!
REST stands for Representational State Transfer, a term first coined in Roy Fielding’s PHd Thesis. It involves using HTTP to manipulate resources identified by URLs via a completely stateless protocol.
The URLs used are generally quite pretty. For example, an e-commerce store might sell a product identified by this URL:
The store might have an API that supplied users with a list of products by performing a HTTP GET request on this URL:
The response might be supplied in an XML document, like this:
<description>A large, left-handed widget<description>
<description>A small, left-handed widget<description>
I mentioned that REST is a stateless protocol, so how can that work in an e-commerce context? People need to browse around the site and fill up their shopping basket!
Simple, a shopping basket is a resource too. Create a basket with a POST request to this URL:
And add items with POSTs like this:
You can use HTTP authentication to ensure that only the basket owner can view or manipulate the contents of the basket.
Enough of fabricated example shops, let’s look at some RESTful APIs that are being used on the internet today...
One of the first mainstream web applications to offer a RESTful interface was del.cio.us with its version 2 API. We’ll be using the del.icio.us API for this example, so if you’re following along be sure to get a del.icio.us account and download CURL, the tool that we’ll be using.
We’ll begin by using the API to get a list of tags that we’ve applied to our bookmarks. Open a command line terminal and type the following (changing username & password to your values):
curl --user username:password https://api.del.icio.us/v1/tags/get
This executes an authenticated GET request for your set of tags and the output will look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<tag count="1" tag="Action"/>
<tag count="1" tag="Bookmarklet"/>
<tag count="1" tag="Day"/>
<tag count="8" tag="EC2"/>
<tag count="1" tag="GTD"/>
<tag count="14" tag="IE"/>
<tag count="14" tag="Maps"/>
<tag count="1" tag="On"/>
<tag count="3" tag="yahoo"/>
<tag count="1" tag="youtube"/>
Pretty cool, no?
Now, let’s examine the REST API to add a new bookmark:
curl --user username:password -d "?url=http%3A%2F%2Fwww.mmmeeja.com%2Fblog%2F&tags=webdev%20web%20design" https://api.del.icio.us/v1/tags/add
The command above will perform a HTTP POST to add this blog :) to your del.icio.us account. Note that the
url parameter is URL encoded and the
tag parameter has spaces replaced by %20.
The observant reader will note that there’s something fishy about the two del.icio.us URLs we just used. The first ended in /get and the second in /add - this goes against the whole idea of using HTTP methods for REST! Yes, delicious have compromised by allowing people to use HTTP GET to create resources and the only way to do that is to break the HTTP model.
This is very common and I understand why they did it, but it’s a shame, especially when I’m using their API to teach about RESTful interfaces. Roy Fielding successfully argues that a resource should only ever have one URL no matter whether the API changes.
So, how about another API that’s a bit more compliant?
I had to search for a long time to find a well-known service that implemented to truly RESTful API, so hats off to BrightKite.
If you’ve not got a BrightKite account already, you might be out of luck because its invite only at the moment, but there are plenty of invites around if you know where to look.
The BrightKite API documentation explains that people, places, notes, photos etc are all resources available via their API. Placenames are rarely unique and unambiguous so places are assigned UUIDs, so the URL for City Square, in Leeds is:
That URL takes you to the web page, but you can get the information in XML format by performing a HTTP GET of the URL with “.xml” on the end:
You should be able to access that part of the BrightKite API without an account, so try it and check out the results. You can also get the results in JSON format by substituting “.json” for “.xml”.
Places can have notes associated with them, get a list of notes attached to City Square like this:
In correct RESTful fashion, we can create a note with an HTTP POST. We must be an authenticated user to be allowed to do this, so replace the
password fields in the command below:
curl -u username:password -X POST http://brightkite.com/places/b1adc0a0b65e11dd8a90003048c0801e/notes -dnote[body]=My%20lovely%20note
Finally, We Get Some REST!
That was part three of the series, only two more to go. I hope that it’s been worth getting up to speed on URLs and HTTP before we got into the REST examples.
Creative Commons licensed photo by Bekathwia.