Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

On first pass, I totally agree with the author. I haven't reviewed the proposed alternative yet but fingers crossed.

That said, I think he misses the single biggest issue with REST APIs that I continuously encounter and which has caused me to consider them sub-par. Representative "State" Transfer. REST APIs are only good for transferring around the state of stateful objects.

However, CRUD operations are only part of the equation in most modern software. Much of what we do is performing actions on those stateful objects (or, better yet in the microservice world, avoid state altogether). REST provides no identifiable mechanism for performing actions outside of CRUD operations and is, therefore, a completely impractical solution. Most real companies' attempts to build a "RESTful" API just end up being RPC over HTTP dressed up to look like REST.



REST provides no identifiable mechanism for performing actions outside of CRUD operations and is, therefore, a completely impractical solution.

The mechanism is identifiable once you start to really think in REST terms. The problem is that people think in RPC, and so try to shove its model instead.

REST is based on names (resources), not commands. Therefore, we must implement our actions as resources. Instead of having a "transfer()" command, you have a Transfer resource type, which you create to start one. Eg:

  POST /transfers ... (Body describes parameters)
  201 Created, Location: /transfers/34
This change from a command to a resource additionally provides the benefits of REST, necessary for communication over an unreliable network: rather than keep your connection open while the action is processed, you get an URL you can poll to check if it is finished. This works even if the client goes offline for some time.


To add to my previous post: it's not like this is a weird concept; every e-commerce site out there implements this. Imagine if we followed the RPC model: the user would click on "submit", and then the browser would wait for a reply when the order was finished being delivered - multiple days later!

Obviously this is impractical, so people follow the natural REST model: they create an Order resource, then send you a link with which you can follow the status of that resource.

But bring the "API" word and suddenly all of the preconceptions push developers to implement everything as function calls.


I'm not sure what you mean by saving that microservices avoid state altogether vis a vis REST.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: