Why REST?

24 November 2007

So you are a new Ruby on Rails developer and trying to figure out what Rails and REST is? Or maybe you touched it before but never really figured it out?..

Ruby on Rails is a rich web application development environment that provides RESTful a development framework right out of the box.

Ok, that’s enough keywords in the first sentence I think :)

What is REST? What does it mean?

I always think that you should know what something means, like the definition of it, as a basic learning point. So here we go.

REST stands for Representational State Transfer. Ph.D Roy Fielding wrote a nice dissertation on the subject called “Architectural Styles and the Design of Network-based Software Architectures” that you can read if you have a spare weekend, for the rest of you, read on.

In my own words, this means that the system making the request does not actually ask for an object’s delete method directly, but instead just says “Hey, person model, delete this record” and leaves the implementation where it belongs, in the model, not in the URL.

So instead of something like:

http://www.myapp.com/people/1/delete

You can have:

http://www.myapp.com/people/1

And pass along with that URL a delete request.

Or instead of saying:

http://www.myapp.com/people/show/1

You can just say

http://www.myapp.com/people/1

Pass a long a show request.

OK… but both the show and the delete look the same, what the hell is going on?

Well, that is both the beauty and the hardest part of understanding REST And why it is even important in the first place. But first a bit of background.

In Dr Fielding’s dissertation, he talks about REST as a method of passing information and requests between systems and instead of coupling the two systems directly, they would just refer to the other system’s available resources passing an identifier and the action they wanted to do on that target system (delete, show, create etc). This meant that two systems would not have to know how the other operated behind the URL and could just communicate via HTTP verbs of GET, PUT and DELETE - and with a standard agreed interface between the systems, means that you would not have to program every interface for every system on a one per one basis.

So, instead of putting the HTTP verb inside the URL (/people/*show*/1) it places the verb inside of the request.

You have probably heard of “GET” and “POST” requests before… they are different ways a web browser can communicate with a web server. One is designed to GET data from the server, the other is designed to POST data to the server.

REST introduces two more verbs, “PUT” and “DELETE”.

Now, web browsers can not say “PUT” or “DELETE” directly, so what Rails does is expects the form comming in to include a hidden field called _method that should equal “PUT” or “DELETE” if you want to use these methods on a RESTful design.

The other way is using Javascript, but not all browsers use that by default, so the hidden field is the guaranteed method.

So, now we have the classic “RESTful routes” table:

HTTP Verb REST-URL Action URL without REST ———– ————- ——— ————————- GET /projects/1 show GET /projects/show/1 DELETE /projects/1 destroy GET /projects/destroy/1 PUT /projects/1 update POST /projects/update/1 POST /projects create POST /projects/create

This table basically summarizes the basics that you need to understand to grok REST.

See how the show request is the same, and the create is also basically the same, but the verb moves out of the URL and goes into the verb?

Then destroy and create us the same real verbs (GET or POST) and get PUT and DELETE passed in as a verb either by Javascript or through a hidden _method field set to PUT or DELETE.

So for you, the Ruby on Rails developer, why would you even bother with REST?

Well, for some sites, you probably wouldn’t bother. Unless you already code RESTfully, there is no real need to if your application is NEVER going to be referenced by an external system not directly under your control.

But if you are making a web application which people could possibly interface into in different ways, making a RESTful App and spending the time learning about it would be very much worth your while.

How do you learn about it?

I recommend (ie, have read and purchased myself) the following in order:

  • Agile Web Development on Rails (THE Rails book you should all read if you are serious about Rails development) This has a good basic introduction on REST and will set you up for the others.

```

-   [RESTful Rails Development](http://www.b-simple.de/documents) by
Ralf Wirdemann and Thomas Baustert translated by Florian Gorsdorf
and Ed Ruder - A free (as in beer) PDF that is 30 pages long
available in English, Spanish and the original German.

```<!-- -->

Anyway, I hope that helped you.

blogLater

Mikel

results matching ""

    No results matching ""