7 ms·
The Restful CookBook
- lisa_henderson 12y agoI just started at a new job, at a new startup. During the my first job interview with them, they asked, "Do you know how to work with a RESTful interface?" I said yes, and I told them to look at my Github account for examples. During my second job interview with them, they asked "What do you consider when you are designing a RESTful interface?" I quoted a bit from the book that Sam Ruby and Leonard Richardson wrote, back in 2007, and which I've read cover to cover. Among other things, I said the overall goal should be to work with HTTP, not against it. Then their lead programmer tried to ask me what he apparently thought was a "trick" question. (He was quitting the startup to go back to school, and I was being hired to replace him.) He asked, if, for a given task, HTTP was too verbose a protocol, then what else might I use for a RESTful interface. I suppose that should have been my first warning. I wasn't sure what they were looking for, but I thought maybe they wanted me to say WebSockets, or perhaps they wanted me to push back against the question and ask why HTTP was too verbose, or they wanted me to suggest the use of TCP in an ad-hoc style that mimicked HTTP. I explored all those possible answers as I spoke with them. Then I was hired and I started the job. Their system is not RESTful at all. The only HTTP verb they use is POST. They use it for everything, and they put all their data in the body of the POST. Their system is pure RPC, with a style influenced by SOAP. The people I'm working with are in their early to mid 20s. I'm not sure if age is a factor, since I've seen the same mistake made by programmers in their 50s. But, for sure, there are some programmers who do not seem to know the difference between RPC and RESTful. Of course, on a personal level, none of that really matters. I really like this place that I'm now working, and I'm happy to work with their RPC style. I have, however, insisted that we use correct terminology, just for the sake of clarity.
- mgkimsal 12y agoI've seen that, and even been "guilty" of it myself years ago - many people take 'RESTful' to just mean anything that returns JSON instead of a full HTML document.
- Drdrdrq 12y agoI usually use the term "REST" instead of RESTful - I know and understand the benefits of pure REST but I choose JSON over XML every time I can. Not to mention suboptimal support for PUT / DELETE / ... methods in Apache.
- nearengine 12y agoREST is an architectural pattern that specifies properties about the interface, not the data - how does using JSON over XML make your interface not RESTful? In his dissertation[1], Roy Fielding always refers to data in terms of resources rather than specific formats. [1] https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arc...
- icebraining 12y agoI think the idea that JSON isn't "REST" is because, unlike XML, it doesn't define links. That said, JSON is just an encoding format. The actual response format is not "JSON", but the specific structure that your clients are expecting to receive (even if it is encoded in JSON), and that can certainly have links.
- aikah 12y ago> Their system is not RESTful at all IHMO ,The problem is nobody has a clue what REST or RESTful really means,at least I don't. But it's clearly not a protocol. It's not even a well defined pattern that exposes clear behaviors with rules. Some developers don't like that, they want clear goals to do their work and want canonical examples they can point to when arguing with colleagues. So personally I understand the appeal for RPC : No semantics , no arguing about whether one should use PUT, POST or PATCH and when , whether links should be headers or in the body, or about which JSON/XML bloated spec to use to make an API HATEOAS...
- icebraining 12y agoREST is well defined, it's just that most guides don't explain it well. REST is just a list of 6 possible characteristics of network protocols/architectures (Client/Server, Stateless, Cache, etc), and an explanation of the benefits gained by having them. That said, it's true that REST by itself doesn't tell you how to build an API. But all of that arguing about PUT/POST/PATCH, links here or there, etc, are mostly bikeshedding. It's like arguing for tabs vs spaces or whether the { should go inline or not. Just read the HTTP spec and then, when in doubt, just pick one and try to be consistent. But you're right that many programmers don't deal well with that. It'd be nice to have some kind of reference protocol using HTTP & JSON and complying with REST, even if it would be as arbitrary as any other possible RESTful protocol.
- mariodiana 12y agoTo a good number of people, "REST" means the Web—but not SOAP. Mapping CRUD to HTTP methods, and so forth, completely escapes some people. "REST good. I want." That's as far as some thinking goes :-(
- petrbela 12y agoWhile some of the tips are useful, I'd much rather see them written with JSON rather than XML. JSON has long been an industry standard. XML is bloated, almost no one uses it anymore. http://restcookbook.com/Mediatypes/json/ http://restcookbook.com/Mediatypes/json/ says "There is no predefined way to deal with link discovery in JSON". I'm surprised it doesn't even mention JSON Schema (http://json-schema.org/ http://json-schema.org/) or JSON API (http://jsonapi.org/ http://jsonapi.org/), which are not official standards but allow you to write machine understandable and discoverable API. If the author wants to promote standards, I think it would be better to look at what the world is using and then put together a guide that explains how to use JSON-LD and other W3C proposals.
- jaytaph 12y agoI think some of the articles actually talk about json-hal and ld. Not sure about schema. The examples are in XML because json is, (and I believe at this point), still not stricly an (accepted) hypermedia capable format, thus not adhering the HATEOAS constraint, and thus cannot be restful. But to be honest, I'd prefer JSON over XML anytime. Please, if you have any idea's on how to improve the site, the whole idea was to set up a small set of articles which others could help out expanding. It turned out that not many people actually posted their idea's (or even complete posts), so what you see on the site is pretty much the same thing as 2-3 years ago. I'd love some more PR's on expanding with up-to-date technologies and techniques (even those who aren't truly restful)
- deathanatos 12y ago> The examples are in XML because json is, (and I believe at this point), still not stricly an (accepted) hypermedia capable format, thus not adhering the HATEOAS constraint, and thus cannot be restful. But to be honest, I'd prefer JSON over XML anytime. What exactly is the criteria of being a "hypermedia capable format"? The linked website gives an example of how the response on a bank-account like API might include links to other actions; XML and JSON seem equally capable here, as they can both encode links. He mentions on another page, > Unfortunately, JSON is not a hypermedia format. There is no predefined way to deal with link discovery in JSON. Is there a predefined way to deal with it in XML? In the earlier example, I had assumed he did exactly what you would do in JSON: make up a field for the purpose. Though, honestly, that would seem to be the purpose of the "Link:" header.
- tomphoolery 12y agoIt's weird to me that a cookbook exists for this, though if it's helping people I have no problem with it. I'm just not sure why we think of "RESTful APIs" as anything other than using HTTP to its fullest capabilities. Even HATEOAS is really just "hey, shouldn't our APIs be as easy to navigate as the rest of the web?". I also find it ironic that there are lots of articles around condemning "the Web" for its idiosyncracies, the biggest offenders are JS folks complaining about how the DOM is implemented (disclaimer: I think it sucks too sometimes), yet these same circles champion things like "REST" and "HATEOAS" and "JSON API"...how are those things not part of "the Web"? Why do we only use "the Web" when we're pejoratively discussing it? All that aside, this book seems relatively well-written. Good job guys!
- brudgers 12y agoThis Software Engineering Radio podcast interview with Stefan Tilkov discusses the difference between REST and HTTP: http://www.se-radio.net/2008/05/episode-98-stefan-tilkov-on-rest/ http://www.se-radio.net/2008/05/episode-98-stefan-tilkov-on-... It is from 2008 but probably more or less evergreen.
- suprgeek 12y agoWhenever I see a REST book I always have the same set of questions: 1) How do I call operations such as Clean, Restart etc not the regular CRUD ones? I may have to end up doing a variation of POST - is that RESTful (whatever that means)? 2) Whats a good way to simulate a "transaction" semantic (In as much as possible over a distributed systems with arbitrary nodes i.e. the Web) 3) How Do I call a long running operation- a create that takes more than a minute for example? Return a "tracking id" and keep GET querying? 4)How do I version my API? What is the one true way? 5)How about automated discovery of the API? Is it SWAGGER or WADL or something else? 6) Is a nested hierarchical URL scheme a good Idea? How much do you nest one resource under the other? 7) Are you a Teapot? Many other questions that I have never seen answered properly
- deleted 12y ago[deleted]
- jaytaph 12y ago1,2,3) Strictly, they are all resources. A "Clean" resource can be created (POST|PUT) describing what to clean. It will create (201) that resource which you can read (get the status), even delete (cancel the action), or change (POST/PUT) again. Take a look at the http://restcookbook.com/Resources/asynchroneous-operations/ http://restcookbook.com/Resources/asynchroneous-operations/, which basically describes this. 4) According to Roy Fielding, you don't (you don't version your website), but you can add/change/delete your resource versioning. I'd (and many) prefer the accept-header for this, but there are as many different versioning systems as there are API's. 5: hypermedia / HATEOAS 6: sure. If it makes sense. Not everything is hierarchical, but things that are, do it. 7: 418 The reason for this was figure out myself how certain things should be done in a "restful" way. To be honest, truly restful is an utopia imho, but on a daily basis, I see many advantages (as well explained in Roy fieldings dissertation) about the restful setup in comparision to a non-rest setup. Pragmatically, since I'm not building API's for HUGE system (and even if they are or will become so), it's still possible to use non-rest components (yes, even verb-like systems and url versioning like /api/v1/. I consider rest to be like the pirates code: not so much a strict law, but more as a guideline.
- glogla 12y ago
- kzhahou 12y agoClient sends a request to delete item #12345 from DB. The item does not exist in DB. Is that a 404, because the resource isn't found, or just generic 400? 404 should be reserved for URL/route errors. We need a few new error codes for common REST operations.
- jewbacca 12y ago> 404 should be reserved for URL/route errors. What dichotomy are you working off of here? It is absolutely a 404. How is this any less applicable to a DELETE request than it would be to a GET request for the same resource? What extra information would this new status status code carry? ---- > 10.4.5 404 Not Found > The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable. http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10...
- kzhahou 12y agoOk, so client requests "DELETE www.company.com/api/thing/1234" and gets a 404. Or even "GET www.company.com/api/thing/1234". Delete vs get doesn't matter. Did 1234 not exist, or is "/api/thing" the wrong path? 404 is overloaded. It can mean two very different things.
- tadaskay 12y ago"is /api/thing the wrong path" - is fixed by hypermedia, so 404 generally means that resource cannot be found.
- jewbacca 12y agoAh, I think I understand what you're getting at now, that's a really interesting question. I can't entirely back this up without probably days of reading RFCs, but my initial gut feeling is that making this distinction, which is not really possible in a truly general way (provided the URI is a syntactically correct HTTP URI), would be exposing internal implementation details in a way that would be conceptually inconsistent with other HTTP response statuses. eg, how to usefully and consistently make the distinction: GET /asfd 200 OK GET /qwer 4XX We don't have that right now GET /zxcv 4XX That is not even a thing Keep in mind that the RESTful URI style is not part of the HTTP standard in any way. In HTTP, URIs can be random gibberish, even if they refer to "sibling" resources semantically. "You're on the right path" vs "What are you even talking about" is not an inherent concept in HTTP. It might make sense in certain circumstances of RESTfully-modelled resource URIs layered on top of HTTP: GET /users/asfd 200 OK GET /users/qwer 4XX We don't have that right now GET /blarghs/asdf 4XX That is not even a thing But to describe this situation in an HTTP response status would be "mixing layers" and making assumptions in a way that opens a conceptually messy can of worms. ---- Disclaimer: I have no idea what I'm talking about and am not entirely convinced by my own argument. I'm always skeptical of "that's just not how it's done right now"-flavoured arguments, which this doesn't stop smelling like as I attempt to reason about it. ---- As practical matter, the body of the 404 response can describe the situation within the context of the application. And if your application is smart enough (though to do this in the general case is impossible or would require something like strong AI), you can use any of several more specific responses (Gone, Found, etc; depending on the context) if you're willing and capable of inferring a specific intent from the otherwise bad request.
- deleted 12y ago[deleted]
- sauere 12y agoSemi-related: https://localize-software.phraseapp.com/posts/best-practice-10-design-tips-for-apis/ https://localize-software.phraseapp.com/posts/best-practice-...
- encoderer 12y agoHaving worked with hypermedia APIs recently I've come to appreciate how they feel more pragmatic and REST more pedantic. The hateos principle in particular lets you build thinner clients.
- JimDabell 12y agoIsn't "hypermedia API" just a new name some people came up with for "REST API" because the misunderstood non-HATEOAS mangling of REST is so common and they wanted to distinguish between the two? Real REST APIs use HATEOAS.
- steveklabnik 12y agoYes. Strictly speaking, you can use hypermedia without REST, too. But that's the lineage of the last few years of what people are talking about.
- encoderer 12y agoSo I'm a so-called "full stack engineer" and the definition I've internalized is that a REST api is essentially about modeling the entities in your application and defining what actions can be taken on each (with consistent use of verbs). And a "hypermedia api" approach is more about modeling behavior. As an example, an api for AirBnB's mobile website might expose a /srp endpoint that can be used by a thin client to render the results recursively. Again, the way I've internalized it, to be a REST api I'd expect a client to request results from a /listings endpoint of some kind. I think there is a lot of confusion and misunderstanding and that precise language is better for everybody. What should we be calling these things? I'd love your thoughts on it.
- steveklabnik 12y agoYour intuition is very good, and gets very close to the heart of the matter. I think that the true heart of the hypermedia approach is actually "how do I deal with change over long periods of time in a scalable fashion?" Everything else flows from this. Exposing behaviors instead of data is one facet of the approach. > What should we be calling these things? Which 'these' are you referring to?
- delinka 12y agoI'd like to request a "recipe" for handling authentication to my API. I want to know: what use is an API key? Should I be authenticating client apps separately from users? (That's users of the application into which the API reaches.) What are the tradeoffs of sending login credentials with every request versus using a token? Maybe this particular cookbook isn't the place for it, but I'd really like to see something cover this. It's a problem that stumps me.
- awinder 12y agoThe purpose of API keys & tokens are to bring user authentication details out of every request. This is helpful in many scenarios: 1. API keys and tokens can be more easily replaced / canceled than entire accounts in the case of being breached 2. API keys & tokens can commonly have additional access restrictions applied, while it's hard(er) to do that for a whole account 3. Likewise, it's easy to think of many-tokens-to-one-account systems, where you encourage users to use different keys for different integrations, which helps lower the surface area of the damage one leaked detail can do 4. Many authorization protocols, like OAuth 2, rely on temporary tokens, which are even more helpful in that they guide application integrations towards not hardcoding sensitive details Just a few reasons & justifications off the top of my head.
- firefoxNX11 12y agoWhile this is a good resource for current RESTful patterns, I found infoq's series of articles on REST http://www.infoq.com/articles/description-discovery-profiles-primer http://www.infoq.com/articles/description-discovery-profiles... better gives a glimpse into the future.
- lisa_henderson 12y agoFascinating that even here on Hacker News, where we can presume the audience is well-informed about tech matters, the comments on this post show a large amount of confusion about the definition of "RESTful". I see some comments raise the issue of XML versus JSON, as if the encoding format impacts on whether something is RESTful. I see others raising the issue of HATEOAS, with some comments suggesting that RESTful is HATEOAS, and others suggesting that RESTful is a different approach from HATEOAS. A term that lacks any agreed upon meaning is of no value. But I wonder why the meaning has become so blurry? I was under the impression that the bible on this subject was the 2007 book by Leonard Richardson, Sam Ruby (and a nice forward by David Heinemeier Hansson): RESTful Web Services http://www.amazon.com/RESTful-Web-Services-Leonard-Richardson/dp/0596529260 http://www.amazon.com/RESTful-Web-Services-Leonard-Richardso... However, reading the comments on this page, I have the impression that many people here have never heard of that book.
- cxa 12y agoWhen I read the title of the post, I wrongly assumed it linked to the "RESTful Web Services Cookbook", which is another O'Reilly classic on this topic. The advice I've heard is to read one or the other, depending on which prose style you prefer. It's nice to see a CC-licensed effort to create a similar resource. It's unfortunate that the names are so similar.
- mariodiana 12y agoI only took a quick look at this, but the first thing I looked at was their discussion of POST vs. PUT. I worry about their answer. My understanding is that the fundamental consideration is idempotency, and what you would like that to do for you. I would have started the discussion there, and then added as a footnote the idea of convenience of working with resources. They seem to have done the opposite.