REST

Posted by Trey on September 28, 2006

I keep hearing about REST and RESTful stuff in Rails, but I don’t really understand. Is it about serving different types of content (HTML, RSS, XML) depending on the device that’s requesting the data?

Trackbacks

Use this link to trackback from your own site.

Technorati

View blog reactions

Comments

Leave a response

  1. Dan Thu, 06 Dec 2007 10:16:21 PST

    No, it’s about using the web protocols correctly. For years, people pretended that HTTP is just a way to make Remote Procedure Calls. For example:

    http://example.com/cgi_program.pl?id=3334&dir=foo&cmd=bar

    This has a lot of problems:

    • It’s not technology independent. What happens when you switch from Perl to Ruby on Rails?
    • It’s impossible to tell if “cmd=bar” is destructive or not. This affects people trying to cache the results, and affects your error retry strategy.
    • It’s legal for anyone to switch the order of parameters, so the document has multiple URLs. This affects the efficiency of the cache.

    Instead, it’s better to put your “resources” (documents, people, whatever) at the center. This isn’t any more difficult to write (at most, one extra config line in your web server, but usually not even that.)

    http://example.com/doc/foo/3334

    If you follow REST, this means:

    • Doing a GET from this URL will never affect the data, so it’s safe to cache. It’s also idempotent, which means it’s safe to restart if there was an error.
    • If we want to replace the data, we can do a PUT. To alter the data, we can do a POST. Caches along the way can easily sniff for POST/PUT and realize the document has probably changed.
    • You could easily make a read-only version of your private app by having a firewall allow only GET requests.
    • The resource (document) has a single URL. This helps later on, when someone builds a mash-up and wants to refer to your documents.

    That’s just scratching the surface. There’s a lot more to it. It’s one of those “things get simpler if you give yourself some limitations”. For example, it might be useful to have a 10ft wide car, but the problems driving down the road would outweigh the benefits.

Comments

Live Preview: