This document discusses REST (REpresentational State Transfer) and common RESTful principles and patterns. It introduces the Richardson Maturity Model, which defines three levels of RESTful design - using URIs, HTTP methods, and hypermedia. The document emphasizes that REST is about representing resources and the relationships between them using hypermedia and standard operations like GET, PUT, POST, and DELETE. It cautions that REST is often misunderstood and implemented incorrectly, focusing on resources and representations instead of remote procedure calls or abstract data types.
1 of 42
Downloaded 11 times
More Related Content
REST - You're Doing It Wrong
1. ReSTyoure doing it wrong!Sergey ShishkinMT AGhttp://www.mt-ag.comhttp://shishkin.org@sshishkin
2. ReSTyoure doing it wrong!Sergey ShishkinMT AGhttp://www.mt-ag.comhttp://shishkin.org@sshishkin
42. Links & CreditsRichardson Maturity Modelhttp://martinfowler.com/articles/richardsonMaturityModel.htmlThe Tale of the Ice-Cream Makerhttp://serialseb.blogspot.com/2009/06/fighting-for-rest-or-tale-of-ice-cream.htmlREST in Practicehttp://restinpractice.com/REST Content on InfoQhttp://www.infoq.com/rest/Images:Web: http://www.flickr.com/photos/zzathras777/1546040168/Mirrors: http://www.flickr.com/photos/coolmikeol/4156970741/Rope: http://www.flickr.com/photos/visualpanic/2759322646/Soup: http://www.flickr.com/photos/wallyg/562283586/Ice-cream van: http://www.flickr.com/photos/estherase/516542355/Ice-cream: http://www.flickr.com/photos/pinksherbet/3398923323/
Editor's Notes
#4: Why should we care about what REST really is?Once upon a time, in the age of fairies and leprechauns, an ice-cream maker named Roy Fielding discovered a seed that people have been using in a remote land, called cacao. Keen on using that discovery, he mixed those seeds with sugar, milk, eggs, cream and sugar and got the inhabitants of his village to try it.Everybody was excited about this new ice-cream. They all knew the ingredients of course, and some of them were using it, but the mixing of all of them in one ice-cream was somewhat a revelation for many. Villagers became very fond of that new ice-cream, and Roy decided to call itchocolate ice-cream. For a long time, many people in Roys village consumed chocolate ice-cream, and all was good in the world. That village was named Web and declared chocolate ice-cream its local speciality.One day, a traveller from the village of Soap, that everybody was calling mister Sainsbury, came to the village of Web and saw how much people liked that chocolate ice-cream. You see, in the Soap village, the local dish was made of soap, and people didnt like soap very much. He decided to bring back the idea in his own village.Sainsbury tried to reproduce an ice-cream people would like, and put milk, eggs and sugar, but because he didnt know about cacao, he decided to use vanilla POD instead. And he got his fellow villagers to try the ice-cream, and they all liked the ice-cream very much, all all was good in the world. And it was named chocolate ice-cream.Mister Sainsbury knew that the rest of the country was still eating soap, and decided, as a good business man he was, that they should all enjoy his chocolate ice-cream. And as mister Sainsbury got so rich from selling soap before, he started promoting his new chocolate ice-cream in the whole country, and soon all were excited about eating chocolate ice-cream made of vanilla.One day, an engineer from the village of Wikipedia came to Web and asked for the national desert, the chocolate ice-cream. The villagers happily provided him with a wonderful cocoa-based chocolate ice-cream. When the engineer started eating it, he started screaming:Engineer -This is not chocolate ice-cream! Its not even the right colour! Do you think Im a fool?Roy This is chocolate ice-cream. It has cacao in it, as it always had.Engineer No nono! Chocolate ice-cream is made of vanilla! Everybody knows that!Roy No, its made of chocolate, if you put vanilla in it its vanilla ice-cream, because theres no chocolate in it?Engineer Youre just an extremist, youre trying to confuse us!Roy And thats when the villagers of Web decided that it was time for them to travel the country, and explain to people the advantages of putting cacao in their chocolate ice-creams.
#5: REST is a set of underlying architectural constraints of the Web the largest informational system ever built, yet simple and scalable.Fieldings dissertation is a scientific paper and thus is not particularly easy to understand and start learning REST.We start with the goals of the web architecture, that REST constraints are aimed to lead to.Further we evaluate common techniques, used by so called (and often self-proclaimed) RESTful services, against contributing to those goals.The goals are:
#6: Web applications are designed to be loose coupled.The web could not ever be deployed as a monolith system.The web is an ever-growing ecosystem of loosely coupled servers, clients and intermediaries.Servers go down, go up or get updated web still works.
#7: The Web have traded feature-richness for simplicity and uniformity.More intermediaries can implement simple and uniform protocol (HTTP) in order to provide features to servers and clients.
#8: Web caching at the proxy-level is an example of a high performance achieved due to the webs uniform interface.Stateless nature of HTTP makes it ultimately scalable as well.
#9: Some dont like chocolate ice-cream.Not everyone needs loose coupling and scalability taken to such an extreme.Not everyone has to build HTTP-based systems RESTful.Do whatever your system needs most. Just dont call it REST if its not.
#10: Which URI is (more?) RESTful?Many developers start designing web APIs with URLs and ask on forums how to design RESTful URLs?This question is nonsense.The Web treats URL opaque.Some websites use so called hackable or guessable URLs for SEO purposes and to allow users overcome some usability issues.Hackable URLs have nothing to do with REST.
#11: Is it REST?No SOAP, no WSDL, no XML namespaces. It should be REST, right?
#12: Is this REST?We got JSON. JSON is RESTful, right?
#13: No. Previous examples tunnel requests through a single URL, burying business logic in the request body.From the webs perspective these are not at all better than a good old WS-* SOAP service.We are still on the lowest level of the Richardson REST Maturity Model.We need resources in order to get a higher rank.
#14: Now we have an awesome resource. Is it REST now?
#15: The previous example did what is called URL tunneling it passes all data, required for the request, in the URL.
#16: This one still uses URL tunneling, but it more accurately chooses the HTTP method.
#17: All the HTTP methods impose particular properties on requests, done with them.Idempotent requests can be retried any number of times and the result should always be as if the request have been handled once and only once.Safe requests should have no side effects that the client can be held accountable for.
#18: Now we nicely defined a user resource and we care about HTTP methods.We GET the user, change his settings and PUT the whole user back.The intent was only to change the user settings, why do I have to PUT the whole user?Some try to solve this problem by standardizing a new HTTPPATCH method, although Web has been living without it all the time.
#19: Resources != Objects, HTTP Methods!=Business Operations.If you find yourself missing domain-specific HTTP methods, think about your resources again.A URI identifies only one resource, but a resource can have more than one URI.
#20: Sometimes people find themselves limited to do CRUD (Create Read Update Delete) APIs because of the limited set of HTTP methods.
#21: Best example of CRUD is Excel: domain logic is in the users head!We want domain logic to be on the server. User should just navigate his way through the workflow.
#22: A better approach would be to model user settings as a resource on its own.
#23: Sometimes APIs are documented using URL templates.This hinders loose coupling.Only URI templates shared out of band are bad. User can safely discover a template by GETting a resource (like Open Search does).
#24: Now we have resources,their URIs and we appreciate HTTP methods.Still we have a long way to go to be RESTful according to the Richardson REST Maturity Model.
#25: When putting the APIs version number into the URL, we couple our clients to that particular version.There is no way we can build a future-proof client for that service.
#26: A much better way of dealing with versions is an HTTP Accept Header.It can incorporate a version number for our specific media type.
#27: The same is with languages.The only valid reason for that is if different languages discriminate unique resources.
#28: HTTP supports Accept-Language Header for that.
#29: If we stick with HTTP features instead of inventing the wheel on top of it, we arrive at the next level of the RRMM.
#31: Generic media types.Clients are tightly coupled to our APIs documentation in order to figure out how to handle the data.Otherwise its just an arbitrary JSON BLOB.
#33: Given we know that the media type describes a product.But we have no idea, what we can further do with it.We are again bound to the documentation.
#34: A better way is to supply next options for that resource in its representation.This is exactly what the web applications do with anchors all the time.We can also use something like an Xform and specify the HTTP method along with required data.
#35: After enabling hypermedia controls in our resources representations we finally achieve the top level of the RRMM.Now our RESTful application looks almost the same as any conventional website: navigate between resources, transitioning the business workflow from state to state. Much like an online shopping process.And there is nothing wrong with it. Websites are in fact RESTful applications.So why do we treat our APIs differently, designing them separately from the UI, putting them on a separate URL?Just provide computer-friendly representations to the resources you human users are already working with through the UI.