RESTful considered harmful
nurkiewicz.com
nurkiewicz.com
but seriously, who would make technology choices based on that reasoning? the thing should have it's merits, preferably for non-shitty software too. so it's not that i agree with everything the author said, i just think your rebuttal is not very good. where were your arguments anyway?
to me, if it's shit, it's shit. but i do prefer shit of the "strongly typed" flavour. when dealing with shit, i'll take any automated help i can get. but even there, of course, we can talk about trade-offs, and not absolute truths.
REST is conceptually really hard and requires almost military discipline.
I suppose you have strange requirements or design impositions that make your life difficult, but I caution you against believing that your use case is universal because in my very real experience, none of your opinions are supportable.
If I hadn't read Fielding's dissertation [1] I probably would have been lost in the weeds as to whatever the heck REST actually meant.
[1] https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arc...
Everything modern is ridiculously complex, even if it looks simple. You can't state that a single thing in a complex system is harmful without forcing yourself to re-evaluate the entire construction of the rest of the system, in which case you can either (A) determine that there is no alternative solution, or (B) the system must be altered in some other way (in which case now multiple things are equivalently harmful. Since (B) happens so frequently, this means many parts of every system are harmful precisely because the system isn't perfect yet (and every part can be minorly adjusted so long as other parts of the system are adjusted - and this latter part is never discussed (probably because the complexity involved is actually complex and someone else would be like 'dude, that's also harmful - your solution has side effects'))).
If it was easy to implement new things people would do that instead of complaining. There's a good reason things are the way they are and not some other way.
There was a small discussion on the issue previously: https://news.ycombinator.com/item?id=9844117
If you don't like it, don't use it.
Partially this is already happening with <whatever>-awesome curated Github repos.
> PS: Obligatory appendix: "Considered Harmful" Essays Considered Harmful. [1]
It's not that HTTP APIs aren't useful - of course they are - it's the religious adherence to REST doctrine that is harmful. I take the author's point to basically be, "do the simplest thing that makes sense for your use case, don't worry about how RESTful it is", which I totally agree with.
Also i don't get many things he is saying like:
> multiple parameters for each criteria, e.g. age=10&name=John&company=Foo (but how to implement OR operator?)
I mean seriously? How would one represent a filter in a GET URL... there aren't many solutions. Also CRUD by Design? What are you talking? REST is not a fucking standard, it's just a way to create your API, you are totally free, to use CRUD or not, your API's could still be within REST if you not use CRUD at all. You could even make REST APIs with GET and POST.
Everything he writes has nothing todo with REST. It's just a design decision.
JSON just happens to be much more readable as well as pretty familiar for anybody who's written code in a C-like language, and especially familiar for JavaScript developers.
NOt in its original spirit, but it was developed in parallel with HTTP 1.1 (see https://en.wikipedia.org/wiki/Representational_state_transfe...) and uses the same verbs (GET, POST, ..) as web browsers to transfer data.
I agree with the posters who say that just using 4 verbs is CRUD-oriented and semantically just not rich enough to support often complex business operations.
Right. HTTP is a (the original and motivating, in fact) RESTful protocol, not a required lower-level protocol on which all RESTful protocols must rest.
One hole in the HTTP method space -- and it results in some weirdness trying to follow REST principles for non-trivial applications over HTTP -- is the absence of a general purpose safe method that takes a request body. (There are some in extensions, such as WebDAV Search's SEARCH method in RFC 5323, but they tend to be overly specific for general use. A generalization of SEARCH to a general purpose, content-type-agnostic query method would be a good addition (much like PATCH was) to the set of HTTP methods.
You do have options though. You can make a GET request but send the paramaters not in the URL but in the body in JSON form. However, that would violate the HTTP/1.1 spec.
As said there aren't many options, one would be a single parameter (jira does that for JQL) and the rest somehow url encoded. However that problem doesn't only happen to rest. Everything on the web suffers from that problem (Jira is a normal Site which doesn't use too much from a Rest API (yet)) so his statement is really falsy.
Uhh... nope. No one forgot that. Least of all the people he's chiding. Any decent Javascript / Front End Dev is acutely aware of this.
Overall, this is perhaps the most unpersuasive clickbait I've read in a long time. It is amusing, though, to see a Java dev complain that REST isn't enterprisey enough. That's not a negative, good sir, that's a selling point.
Long story short - we ended up with http://jsonapi.org/ - a complete specification for building JSON-based APIs. It supports such things as pagination, filtering, sorting, relationships, etc. All of us could implement it all very easily, but only God knows what is on the mind of an engineer, i.e. whether he likes /endpoint.json vs /endpoint?format=json.
I thought this article was OK - certainly good enough to skim read for 5 minutes. Neither an upvote nor a downvote from me.
Personally, "considered harmful" is in the "meh" category for me, but I'm not curating HN articles, nor is any other individual.
REST has its limitations, but it has one nice advantage: it uses a very well-understood, totally ubiquitous, firewall-piercing protocol. I suppose it's still a huge win for any public-facing, moderate-load service. One can use compact (non-JSON) data representation and even compress headers to save bandwidth.
Is human readability a main reason for using JSON or is it a beneficial side effect?
I always assumed (rightly? wrongly?) that the reason for using JSON was that it uses Javascript's native object format and therefore much easier to serialize/deserialize and debug at the browser level.
Nobody in their sane mind would `eval()` a JSON received from network, so parsing is required in any case, JS or not. A combination of lists and maps (maps being just a special-form list of pairs) can be represented in a much more compact way, especially numbers. Thrift and Cap'n Proto are definitely more compact and as fast, or faster, to parse.
I still suspect that in many cases, HTTP headers can be comparable to the message in size. A high-efficiency protocol would create a connection and avoid re-sending this information with every request — but good luck taking it through certain corporate firewalls.
My assumption was more related to the intent behind choosing JSON for REST, not so much related to the actual implementation(s).
REST is a protocol-independent architectural style. (And, even if you mean the kind of RPC over HTTP that is often misnamed REST, then using "a very well-understood, totally ubiquitous, firewall-piercing protocol" -- i.e., HTTP -- isn't really an advantage over the main things it is an alternative to, e.g., XML-RPC and SOAP-over-HTTP, which use the same "very well-understood, totally ubiquitous, firewall-piercing protocol.")
I'd love to see a relatively non-obscure example of REST run over to a non-HTTP protocol. REST feels sort of natural over HTTP, with all these URIs and HTTP method verbs.
XMLRPC has all the disadvantages of REST listed in the original article, but seemingly none of the advantages (like a logical, discoverable structure). Correct me if I'm wrong.
SOAP indeed has advantages — it has WSDL! When done correctly, it's delightful to work with. It's complicated and enormously more bloated, though. When run over HTTP (that is, most of the time), SOAP suffers from the same transport- and connection-related disadvantages as REST over HTTP does.
HTTP itself is the prototypical example; it is REST over an unspecified lower-level transport protocol (usually, in practice, TCP/IP, but that's explicitly not required in the spec) in the same sense that REST APIs that we usually talk about are typically REST over HTTP.
Most of these complaints boil down to REST is not complex enough to capture more complex interactions. But that is its strength -- yeah, there might be some tweaks to make but if you run into a RESTful interface it is easy to get your bearings.
If you need something with more meat, pick something else.
That said, shoehorning problems into a limited number of return codes with pre-existing specific meanings is an inherent problem with the whole REST approach, I'll give the author that. It is almost never a good fit.
I was surprised he didn't mention ease of use inside JavaScript.
It may or may not be the same to parse, say, JSON and XML.
But how is
xmlDoc=new DOMParser().parseFromString(booksXml);
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
as easy to use as
JSON.parse(booksJson).title.edition = 'first'
?
This advantage has nothing to do with eval(), but rather that JSON is pretty much built-in everywhere nowadays.
I can just think in XML easier than remembering "Okay, so there's a bracket here, and a curly brace here, or is it curly brace and bracket? Do I need to include a key value here or not? Oh crap, I overlooked a comma again." I constantly have to send my JSON through lint, where I don't ever have to worry about that with XML.
I have never run into HATEOAS in the wild but it sounds pretty cool. I hope to get to try out a service that implements that one day.
Dismissing such criticisms with "It works for me," "If you don't like it, don't use it," or "Who cares about X? I just want my Y to work!" is annoying and by definition anathema to any technical discourse. They're vapid emotional arguments.
At StackHut (http://www.stackhut.com) we're hacking on a platform that takes code you've written and hosts the API in the cloud over JSON-RPC. We think it's pretty cool and would love to hear from others doing interesting stuff with JSON-RPC.
I'd just like to point out that none of JSON, Protocol Buffers, Avro, or Thrift are protocols. They're serialization/unserialization formats/libraries/frameworks of greater or lesser tonnage. Whether they suck or not largely depends on how they're used; if you're tunnelling all of your communications over HTTP, you might want to do something HTTP-friendly.
"Not to mention Swagger, the de facto standard for REST documentation, officially claims such approach is "not per design" - and sticks to fixed URIs."
What? (One of the great downsides of REST is that most of the tools that claim to support RESTful designs, including JSR311 and apparently Swagger (I've never used it) simply don't.)
discoverable != must be discovered
This is a misconception and criticism I used to have. However, it's a misunderstanding of the point of HATEOAS - the point isn't that you can't use hard-coded URIs, but that any given URI should be reachable by traversal from the root URI.
In other words, it's fine to keep a bookmark to your favorite article, just make sure you can get to that article from the homepage, too.
(I guess I would say modulo redirects as well)
I belong to the static typing camp...
REST, static/dynamic typing... all decisions like this are a cost trade-off.There aren't really two 'camps', there are just comfortable positions on a scale, places where people are acclimatised to the trade-offs by their experience.
I just built an API over HTTP, ignored all the REST rules and separated everything into commands and queries. Commands are POST and do something. Queries are GET and get something. I need to write this up.
Schema is hell with JSON etc. I don't get how you can enforce something like this cleanly in a language like JavaScript under node.js or something without piles of assertions or some weird meta-format. I'm barely managing with an informal
Also JSON is a crapfest for transferring data. Need something better than that over HTTP that is strongly typed with strong schema and has flexible binary formats other than whacking a base64 encoded string in an object or using multi-part mime.
And then you're back at RPC and protobufs or something similar. Perhaps that's the answer.
Simply a command is something mutative. A query is something not mutative that returns information. There is no further specification other than that.
It doesn't mandate a bus or event queue or publishing model nor whether or not the commands are synchronous or asynchronous.
Greg
I think the title misses the point a little, or at least generalises too much. REST is not harmful, it's incredibly powerful when used for the right thing. However, REST is not the correct technology choice for internal or high performance APIs.
One, the API used typical GET, POST, PUT, etc.. The second (before I knew the term applied to this) provided a single URL whose response was needed to describe the other operations. I remember instinctively hating the second. Now MY code has the burden of translating these into the specific calls. However, now we are version proofed.