views:

248

answers:

5

*update: horray! so it is a journey of practice and understanding. ;) now i no longer feel so dumb.

I have read up many articles on REST, and coded up several rails apps that makes use of RESTful resources. However, I never really felt like I fully understood what it is, and what is the difference between RESTful and not-restful. I also have a hard time explaining to people why/when they should use it.

If there is someone who have found a very clear explanation for REST and circumstances on when/why/where to use it, (and when not to) it would benefit the world if you could put it up, thanks! =)

+1  A: 

This article does a good job classifying the differences in several http application styles from WS-* to RESTian purity. What I like about this post is it reminds you that most of what we call REST really is something only partly in line with Roy Fielding's original definition.

InfoQ has a whole section addressing more of the "what is REST" angle as well.

In terms of REST vs. SOAP, this question seems to have a number of good responses, particularly the selected answer.

Grant
I would add this for an explanation within the context of Rails. http://guides.rubyonrails.org/routing.html
Beerlington
Agree, InfoQ's REST section is an excellent source to understand REST (link if provided by @Grant). 'How to GET a cup of Coffee' (http://www.infoq.com/articles/webber-rest-workflow) is my favorite and one of the first few articles that helped me understand REST concepts.
Surya Suravarapu
+14  A: 

REST is usually learned like this:

  1. You hear about REST being using HTTP the way it was meant to be used, and from that you shun SOAP Web Services' envelopes, since most of what's needed by many SOAP standards are handled by HTTP in a simple, no-nonsense way. You also quickly learn that you need to use the right method for the right operation.
  2. Later, perhaps years later, you hear that REST is more than that. REST is in fact also the concept of linking between resources. This often takes a while to grasp the full meaning of, but when you learn this, you start introducing hyperlinks into your responses so that clients can navigate your system without being coupled to how the server wants to name its resources (i.e. the URIs).
  3. Even later, you learn that you still haven't understood REST! And this is because you find out that media types are important. You start making media types called application/vnd.example.foo+json and put hyperlinks in them, since that's already your understanding of REST.
  4. Years pass, and you re-read Fielding's thesis for the umpteenth time, to see if there's anything you missed, and it suddenly dawns upon you what really the HATEOAS constraint is: It's about the client not having any notion of how the server's resources are structured, but that it discoveres these relationships at runtime. It also means that the screen in front of the user is driven completely by what is passed over the wire, so in fact, if a server passes an image/jpeg then that's what you're supposed to show to the user, not an error message saying "AtomProcessor can't handle image/jpeg".

I'm just coming to terms with #4 and I'm hoping the ladder isn't much longer! It's taken me seven years.

mogsie
Keep going looks like you are getting close. But then...I have thought that myself many times only to have yet another epiphany :-)Great to see people digging deeper! I hope it was as mind blowing for you as it was for me.
Jan Algermissen
Absolutely! What's the next step?
mogsie
What a wonderful answer. Your path has been very similar to mine.
Darrel Miller
Accepted. Not the "textbox answer" i was originally looking for, but a very real/human perspective into what tends to be spoken off in overly deterministic language
ming yeow
+1  A: 

I would imagine YMMV, but I found it very easy to start understanding the details of REST after I realised how REST essentially was a continuation of the static WWW concepts into the web application design space. I had written (a rather longish) post on the same : Why REST?

Dhananjay Nene
+1  A: 

Scalability is an obvious benefit of REST (stateless, caching).

But also - and this is probably the main benefit of hypertext - REST is ideal for when you have lots of clients to your service. Following REST and the hypertext constraint drastically reduces the coupling between all those clients and your server, which means you have more freedom when evolving/developing your service over time - you are not tied down by the risk of breaking would-be-coupled clients.

On a practical note, if you're working with rails - then restfulie is a great little framework for tackling hypertext on the client and server. Server side is a rails extension, and client is a DSL for handling state changes. Interesting stuff, check it out here: http://restfulie.caelum.com.br/ - I highly recommend the tutorial/demo vids they have up on vimeo :)

Mike
A: 

Content-Type: text/x-flamebait

I've been asking the same question lately, and my supposition is that half the problem with explaining why full-on REST is a good thing when defining an interface for machine-consumed data is that much of the time it isn't. OK, you'd need a really good reason to ignore the commonsense bits (URLs define resources, HTTP verbs define actions, etc etc) - I'm in no way suggesting we go back to the abomination that was SOAP. But doing HATEOAS in a way that is both Fielding-approved (no non-standard media types) and machine-friendly seems to offer diminishing returns: it's all very well using a standard media type to describe the valid transitions (if such a media type exists) but where the application is at all complicated your consumer's agent still needs to know which are the right transitions to make to achieve the desired goal (a ticket purchase, or whatever), and it can't do that unless your consumer (a human) tells it. And if he's required to build into his program the out-of-band knowledge that the path with linkrels create_order => add_line => add_payment_info => confirm is the correct one, and reset_order is not the right path, then I don't see that it's so much more grievous a sin to make him teach his XML parser what to do with application/x-vnd.yourname.order.

I mean, obviously yes it's less work all round if there's a suitable standard format with libraries and whatnot that can be reused, but in the (probably more common) case that there isn't, your options according to Fielding-REST are (a) create a standard, or (b) to augment the client by downloading code to it. If you're merely looking to get the job done and not to change the world, option (c) "just make something up" probably looks quite tempting and I for one wouldn't blame you for taking it.

telent
The problem with the Fielding method is that it makes it hard to build applications on top of existing services; he has no deep vision of meta-services at the application-level (unlike at the network level). As such, he's effectively calling for an entire eternal cottage industry of people who spend their time clicking through forms to automate things. That's not the right level at all to be outsourcing anything! Conceptually repetitive stuff should be fully automated, and there ought to be a standard way for RESTful webapps to describe themselves (if they wish; it should be optional).
Donal Fellows
I know some people recommend strongly to use standard media types, but I believe that is more due to the fact that it is much harder than people think to create a well designed hypermedia media type. Can you point to some authoritative document that says "thou shalt no create your own media types"? My perspective is that if you understand why standard media types are so important to Rest then you are in a position to make the choice whether to create your own.
Darrel Miller
@Darrel I can point you at at Roy Fielding's blog: http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven. This comment form is too short to quote it fully, but scan down to the last bullet point starting "A REST API should be entered with no prior knowledge beyond the initial URI (bookmark) and set of standardized media types" and read that para in full.And I agree with you about the work involved in defining a new one: Tim Bray has waxed eloquent on that subject already. I just don't see how Magic is expected to happen if you stick to types that other people have made
telent
@telent, I believe that @Darrel is well aware of the constraints of REST; I am also looking for an authoritative source stating that "thou shalt not create your own media types". If I have a problem domain, and there are no media types that resonate, I mint my own media types. Of course that's the hardest part of REST, defining good hypermedia types! Tim himself was involved with minting thirteen so media types himself: http://www.tbray.org/ongoing/When/200x/2009/03/16/Sun-Cloud (I now feel embarrassed about my own comment from way back then, what I'm on about has nothing to do with REST...!)
mogsie
Fielding says "A REST API should be entered with no prior knowledge beyond the [...] standardized media types". I can't read that in any other way than as saying that if you invent a new resource type for your app (unless you go on to make it a standard) he considers your API non-REST. Do you think this is not explicit enough, or do you not consider Fielding's blog a sufficiently authoritative source?
telent
And I agree with you that this is the hardest part (at least, to do properly) and that's *why* I suggest that unless your service is going to be long-lived and world-famous it's the bit that may well provide least RoI. The other tenets of REST can be profitably adopted 90%+ of the time, but this is the area where you really need to think about your goals and whether the time you sink on it will win you anything
telent
@telent, I would have you read this: http://roy.gbiv.com/untangled/2008/paper-tigers-and-hidden-dragons and perhaps Joe Gregorio's response http://bitworking.org/news/365/beyond-rest-and-resource-design. How would you do such a thing without designing new representations? Serendipitous reuse can occur at small levels too, like inside a department, or inside a smallish company even. REST has a role in these areas too. I can standardize (e.g. by ivory tower, or grassroots) a media type e.g. within an industry easier than I can get it ratified by the W3C or IETF.
mogsie
Interesting. In this article Fielding defines a representation that *doesn't* use discoverable hypermedia (i.e. is not RESTful). If he had gone on and put the links and forms in to make it hypertext, what would be the actual benefit? Unless he also provides the out-of-band information (by publishing a standard of some form) describing the actual semantics of those links, the only value of the markup is (to paraphase Donal) for people who want to write a client by guesswork.
telent