Live data from Hacker News

Nobody Understands REST or HTTP

blog.steveklabnik.com

101–110 of 141 posts

Re: Nobody Understands REST or HTTP

#101
post #94
post #92

Earlier quoted context omitted.

Upvoted because I like your solution for pagination and for further information via an :href attribute. However, I don't see a real problem with the way Rails does things because it's "by default" and I think a dev ends up causing a lot of unnecessary trouble for himself by using stock to_json. The more I work with Rails building out a JSON API, the more I realize that .to_json is just another way to "scaffold" - it'…

I can see that point ... but where's the direction on what to do next? What's the post-scaffold JSON mechanism? Bonus points if it lets me generate/embed urls with the knowledge/techniques I have from making web pages. If you say "erb", please don't be offended by me laughing.

Perhaps something along the lines of the following would provide a good balance http://quickleft.com/blog/presenters-as-a-solution-to-asjson...

It's also worth noting that to_json should not be invoked directly these days in favor of as_json. It's now meant to more or less be an internal Rails API.

Re: Nobody Understands REST or HTTP

#102
A very good and comprehensive writing.

One thing that I cannot find mentioned neither in the article nor in comments, is the confusion related to the idea of state.

While REST is, by definition, stateless, it merely describes the session, not the manipulated data. There's no way to avoid, at least implicit, object's state. For example, if user's password is not empty, it means that the user already set the password.

More comprehensively, with /users/1/edit, or whatever, it is frequently expected to get a different validation error, depending on the flow, such as a paid account might require more billing related details than one without email confirmed.

Of course, it is a nightmare to maintain such matters implicitly. That's why Ruby on Rails plugins like acts_as_machine, and similar, come handy. They do provide an explicit description of the object's behaviour, easy to transform into documentation, depending on the current state.

The idea shows its full potential with multipage forms. Each step of the form might be described with a different state, stored in the state attribute, leaving the controller RESTful, and the model's validations easy to understand.

A yet another confusion comes with too close database mapping. To stick with Ruby on Rails, there is nothing preventing the developer from creating his own setters and getters, like def vattr=(); end; def vattrr(); end.

The framework passes the value of a virtual attribute, using params, to #update_attribute, and other methods, the same way it does for regular attributes created automatically from the database's schema.

Very frequently such attribute provides a much cleaner way to represent the model, rendering either the HTML or the API much more easier to write or understand.

Re: Nobody Understands REST or HTTP

#103

Earlier quoted context omitted.

Did you read the article? No matter nouns or verbs but in your example you are doing an HTTP GET. Does get getallpages sound right to you? I did not downvote you but the REST architecture is well thought out, simple to follow, and very sufficient if you are moving content over HTTP.

First off, thanks for taking the time to reply to my admittedly baiting post. I do have a serious point I'm making though. Lets walk through it. http://www.mywebsite.com/api/getAllPageNames/returnType/JSON In this string 'get' appears once. However, as an experienced dev, you know that this is a GET request. To you it looks redundant. However to someone trying to understand what will happen when they hit enter with t…

> The question at that point is naturally "But if you don't know that's a GET request then why are you using an API?"

Your efforts to build what you perceive as the more accessible API is appreciated. However please take the time to understand the alternate viewpoint, it might be useful sometime.

The whole point of REST is I mostly do not have to study another API documentation. The "uniform interface" is not a limitation, it is a feature.

I suspect you may be underestimating your user-base. If your website has useful enough content, somebody is bound to write a programmable interface, RESTful or not.

Re: Nobody Understands REST or HTTP

#105

The biggest problem with today's REST implementations is that they're essentially a database serialization layer. Consider how a RESTful Rails model is typically represented as: { book: { id:1, name:"To Kill a Mocking Bird", author_id:2 } } How do you get more info on the author if you only have this piece of information? Rails/ActiveResource guesses through convention: "/authors/2", but that might not be the case, w…

The GitHub API uses the Link header for that.

    $ curl https://api.github.com/repos/github/gollum/issues -i       
    HTTP/1.1 200 OK
    Content-Type: application/json
    Link: ; rel="next", ; rel="last"

Re: Nobody Understands REST or HTTP

#106
I thought I finally had REST figured out until I read this.

Does anyone else see a diminishing return on pedantically following the REST-prescribed design and actually creating an API that people are familiar with out of the gate? I feel like if I followed this article to the T in my current design, anyone trying to integrate with it would spend so much time reading my API documentation just to figure out why they weren't getting an XML reply that it would stop being productive.

I am working on an API for imgscalr right now and I think I have it about 80% RESTful at the moment -- the parts I'm not doing is paying any attention to the headers either coming or going between client or server.

I'm using the more recent "classic" /path/object.{format}?arguments format that I have seen folks like Wordnik and Vimeo use.

It may not be purely RESTful, but it is intuitive and I can't help but think: "Remember 3-4 years ago when REST wasn't all the craze and no one knew what it was? The web still managed to function back then..."

I'm trying to mix some practicality in a bowl filled with "best practices" and come up with something that most people can stomach.

I hope I hit that mark, but we'll see. As far as I'm concerned I'm sticking with the versioned URLs because I find it more intuitive for endpoints that change, get renamed or are removed than slamming one inside of a mime type or header or even a query string param a la AWS.

Re: Nobody Understands REST or HTTP

#107

This is an excellent example of a spec being deemed more important than a useable system. Frankly, I don't give a damn about the REST spec, and neither should you. When I see an API I want it to be simple and clear. /getAllPages/ Hey, it's a verb and I've got a pretty good idea of what I'm going to get back /getAllPageNames/returnType/JSON Now I really know what I'm getting. The simplicity and usability of this struc…

GET /pages.json?

Re: Nobody Understands REST or HTTP

#108
post #93

Earlier quoted context omitted.

I disagree that "REST is just kinda that [database serialization]"; it is one approach to REST, but its brittle and tends to bias developers towards over-engineering their API into these tiny little pieces that correspond with each database table or model. I'm designing the API for Poll Everywhere right now. Internally a multiple choice poll resource consists of several database tables and Rails models, but I simplif…

I've ended up doing without ActiveRecord's to_xml/to_json to (among other things) accomplish HATEOAS within Rails. Instead I'm using xml builder and a roughly equivalent json-from-ruby-hashes template system. The process is a bit ugly at times, but so far the results have been pretty promising. We've already had api changes break enough internal clients that people are pretty excited about that never being their prob…

Do you have any bits of this code open sourced?

Re: Nobody Understands REST or HTTP

#109

Rest was a really big deal in the Java web-app world for about a year... It was back when early versions of Struts were 'the designated alternative', and in the bad old days of J2EE and JAX-RPC. Then Java EE 5 came out, which solved large chunks of the pain that J2EE inflicted, JAX-RPC got replaced with the infinitely better JAX-WS and there was a new breed of web frameworks as well, which blew Struts out of the wate…

Where to start with a comment like this?

'...REST always been a part of rails?' '...obsolete technology'

How is REST -- a set of architectural principles and constraints -- obsolete when you use it every time you browse to a web page? I feel people need to start treating REST more for what it is rather than an API or some library, or SOAP for that matter.

Re: Nobody Understands REST or HTTP

#110
post #97

Earlier quoted context omitted.

1) HTML isn't English, it just has some similar sounding tokens. You can't assume the semantics are the same. 2) Well, there's HTTP and there's REST, they're related but no the same. HTTP is a full protocol, completely defined; you can't add more verbs, period. REST is an architectural style . It has a series of constraints, and it tells you that if your system architecture complies to them, it'll have some useful pr…

to #4 - that's only true if you designed your app to rely on a single server and have no fault tolerance or distributed sessions. Argh.

The original stateless way has fault and partition tolerance baked right in. By breaking this convention you open yourself to a world of hurt, no matter how many band aid concepts you patch over it.

If you had ever designed or worked on a distributed system that needed to scale you wouldn't talk about adding servers and sharing state (distributed anything).

Sure distributing state is a fail-safe strategy - but not a scalable or performant one.

Post reply on HN