top | item 3775757

RESTful thinking considered harmful

97 points| Titanous | 14 years ago |shopify.com | reply

73 comments

order
[+] cletus|14 years ago|reply
Look, I'm not a REST cheerleader by any stretch of the imagination but in REST terms the OP just doesn't know his ass from his elbow here. Consider:

> POST /orders/42/pay

> POST /orders/42/ship

You don't put verbs in your URLs. This is really REST 101. I highly recommend Nobody understands REST or HTTP [1]. The above is exactly why you don't put verbs in URLs. Consider instead:

    POST /payment { orderId=42, etc }

    POST /shipment { orderId=42, etc }
is a much sounder RESTful API design.

And in case anyone feels the need to suggest SOAP, I highly recommend the satiricial (but disturbingly accurate) The S stands for Simple [2].

[1]: http://blog.steveklabnik.com/posts/2011-07-03-nobody-underst...

[2]: http://wanderingbarque.com/nonintersecting/2006/11/15/the-s-...

[+] bluesnowmonkey|14 years ago|reply
Maybe you just skimmed the article, or he's changed it since, but he doesn't say that verbs in URLs was RESTful. He said it was better than RESTful, and I'm inclined to agree with him.

Having tried my hand at designing REST APIs for typical business systems a few times, I've noticed that state machines and transactions are everywhere. Making every resource a noun with the four basic REST verbs leads to a complex and extremely chatty design, whereas slipping a few extra verbs in here and there makes the API simpler, easier to understand, and more efficient.

[+] jeffmess|14 years ago|reply
Placing verbs in your Resftul apis resources is wrong, but it just feels so right....
[+] debacle|14 years ago|reply
My thoughts:

1. Most people who think that REST is The Best Thing Evar don't really understand what REST is.

2. Most people who think that REST is terrible don't really understand what REST is.

REST is a tool. It's useful for a lot of things. It can alleviate initial scalability issues. It can make an API easier to use or require fewer calls. It can make it easier or more intuitive for someone to learn an API, and it's very often easier to make changes to a REST API because of the way it is written.

But it's just a tool, and if you can't grok that, you're probably a tool as well.

[+] ajross|14 years ago|reply
So only true scotsmen aren't tools? I'd argue that if a concept is so badly explained that both its most zealous proponents and antagonists are mistaken, that perhaps it's not such a useful design concept in the first place.

I mean, who could possibly be confused about what the transfer of representational state means?

[+] pradocchia|14 years ago|reply
> But it's just a tool, and if you can't grok that, you're probably a tool as well.

Unnecessary. All tools have a learning curve, a conceptual slope leading to the sweet spot. Being low on the curve does not make one a tool.

[+] krober|14 years ago|reply
I agree with the "it's just a tool" sentiment in general, but what other great alternatives are there? I haven't had a favorable experience with SOAP, so I can understand why people think REST The Best Thing Evar. This is based on my own experiences, so I may have used SOAP wrong, misunderstood its intentions, etc. Do you have any examples of when SOAP (or another technology) is preferable to REST?
[+] webjunkie|14 years ago|reply
"The RESTful thinker may design both the payment of an order and the shipping of an order both as updates"

Maybe this thinker got it wrong. How about POST /orders/42/payment to create a payment object?

[+] hsuresh|14 years ago|reply
Yup. The author is wrong in his assumption. You can always model the "process" parts using a resource(payment resource as against pay, transaction as against credit/debit etc).
[+] masklinn|14 years ago|reply
> to create a payment object?

resource.

Also, not sure what's up with TFA's strawmanning,

    PATCH /orders/42 # with { order: { paid: true  } }
    PATCH /orders/42 # with { order: { shipped: true } }
looks like RPC update calls (hey let's map our Ruby attribute updates to HTTP calls, yay!) more than RESTful calls.

Also, not sure why a payment resource would be conceptually "within" an order (although that's got little to do with RESTfullness): why not POST to /payments?order=42? Which would return a link to e.g. /payments/2345 for your actual payment resource?

Something similar to http://blog.steveklabnik.com/posts/2011-07-03-nobody-underst... transaction resources.

[+] beza1e1|14 years ago|reply
Nearly correct. POST does not specify the name of the resource. So you do

  /orders/42 # with { order: { paid: true  } }
And it returns the /orders/42/payment transaction resource.
[+] crewtide|14 years ago|reply
REST is a great starting point for an API, but generic REST interfaces (in Rails, Django, or any other framework) are relied on too heavily. I'm especially interested in mobile APIs, which have very specific considerations that REST doesn't answer well.

Building a generic data back-end for mobile is very hot right now, with lots of funded startups in the space. Some of them are just offering a REST API, which frankly any Rails or Django developer could produce in about 10 minutes. Mobile APIs need to be different, mostly due to slow or non-existent connections, and require additional thinking. More details here: http://ow.ly/9YMrM

Anybody know of any mobile back-end providers that are thinking innovatively about this? I've consulted a few of these startups and they all seem hesitant to make any choices that go beyond REST...but as far as I'm concerned, offering a generic solution for something that mobile devs have to develop every time they create an API would be well worth it. A simple example is to accept a guid for every created object and return that guid in the response. It's something you have to do to know that the object has been sent successfully to the server, so why make the developer code it every time?

[+] beza1e1|14 years ago|reply
I do not think your blog examples goes beyond REST. You just need to represent different resources. For example a "title list" or "article set".
[+] RyanMcGreal|14 years ago|reply
> POST /orders/42/pay

> POST /orders/42/ship

But "pay" and "ship" are actions, not objects, so it doesn't make a lot of sense for them to be represented as URLs.

[+] rch|14 years ago|reply
You hit the nail on the head there. If the author posted a new payment (using whatever metaphor), then a handler could initiate a set of actions in the course of processing <metaphor>.

Maybe this is a real debate in some circles, but I cannot fathom mapping HTTP verbs directly to SQL statements.

[+] dalore|14 years ago|reply
> POST /orders/42/payment

> POST /orders/42/shipment

Fixed?

[+] kevinpfab|14 years ago|reply
Exactly. Having these endpoints instantly turns your API into an RPC-API. This is the start of a snowball of complexity introduced for your consumers.
[+] silon4|14 years ago|reply
POST /orders/42?pay=1

POST was always about actions, not just resources.

[+] chrisdinn|14 years ago|reply
Small thing, but when the author attributes REST to Robert Fleming, I believe he actually means Roy Fielding.
[+] MatthewPhillips|14 years ago|reply
Can we please kill the "considered harmful" meme?
[+] jerrya|14 years ago|reply
X considered harmful is one of computing's oldest memes, 44 years old this month, and was well sort of co-created by two of its foremost computer scientists. (Dijkstra and Wirth)

As memes go, it has a long history.

http://en.wikipedia.org/wiki/Considered_harmful

[+] gmaslov|14 years ago|reply
Why? When I see "considered harmful" in a title, I know pretty much what kind of article to expect. I think it's a useful shorthand, and that its origins, and the whimsical attitude associated with it, are irrelevant. What alternative title would you suggest for this article?
[+] Tobu|14 years ago|reply
Especially here where it's cheap linkbait for an article whose actual claim seems to be “Gee, REST is hard to get right sometimes”. Not that I like the inflamed rhetoric of an actual “considered harmful” call to arms, either.
[+] strictfp|14 years ago|reply
The writer seems not to consider several other models such as

PUT /orders/42/payed or POST /orders/42/paymentprocessor

, which in my eyes would be both restful and true to the business model.

[+] hkarthik|14 years ago|reply
Ship with a state machine implementation by default, and a generator for a state machine-backed process model. Be opinionated!

I think this is the most compelling take away. I learned how to use the state_machine gem in my current job, and it's an invaluable tool for going beyond CRUD and building real process models.

[+] headbiznatch|14 years ago|reply
State machines are excellent, but this comes up every so often on HN and I've come to respect the observation that state machines become very difficult to change.

They particularly rock for games, where the universe is unlikely to suddenly add new interactions or change fundamental laws, but in an evolving business application with requirements that perpetually jag, they can be a source of great pain.

EDIT: After thinking about this a bit more, I realize the state machine use is in reference to an API, which probably should be approached with a certain sense of immutability. I still think that in general, using them outside of games should be approached with serious fore-thought to the potential for change in your requirements.

[+] iamleppert|14 years ago|reply
Is it just me or are these "considered harmful" articles becoming way too cliche? When I see one, I immediately become suspicious of it's content and distrust it as I've come to associate it with some sort of inflammatory headline or flippant way to dismiss legitimate conversation.