views:

339

answers:

8

I've been reading up on REST, and I'm trying to figure out what the advantages to using it are. Specifically, what is the advantage to REST-style URLs that make them worth implementing over a more typical GET request with a query string?

Why is this URL:

    http://www.parts-depot.com/parts/getPart?id=00345

Considered inferior to this?

    http://www.parts-depot.com/parts/00345

In the above examples (taken from here) the second URL is indeed more elegant looking and concise. But it comes at a cost... the first URL is pretty easy to implement in any web language, out of the box. The second requires additional code and/or server configuration to parse out values, as well as additional documentation and time spent explaining the system to junior programmers and justifying it to peers.

So, my question is, aside from the pleasure of having URLs that look cool, what advantages do RESTful URLs gain for me that would make using them worth the cost of implementation?

+3  A: 

One that I always like as a savvy web-user, but certainly shouldn't be used as a guiding principle for when to use such a URL scheme is that those types of URLs are "hackable". In particular for things like blogs where I can just edit a date or a page number within a URL instead of having to find where the "next page" button is.

Daniel DiPaolo
All URLs are hackable. If you're relying on people not guessing your URLS as some kind of security mechanism, you're doing it wrong and the URL scheme is the least of your problems.
meagar
I think Daniel meant this more like rather than navigating your bloated, nonsensical UI, he can find stuff by modifying the URL. That's certainly one reason *I* prefer RESTful URLs.
notJim
@notJim That's precisely what I meant. If your pagination controls suck, it doesn't matter as much to me if I can turn `http://foo.com/article-name/page/1` into `http://foo.com/article-name/page/2` easily instead of having, say `http://foo.com/display_article.php?item=412551`
Daniel DiPaolo
+6  A: 

One way of looking at REST:

http://tomayko.com/writings/rest-to-my-wife

So anyway, HTTP—this protocol Fielding and his friends created—is all about applying verbs to nouns. For instance, when you go to a web page, the browser does an HTTP GET on the URL you type in and back comes a web page.

...

Instead, the large majority are busy writing layers of complex specifications for doing this stuff in a different way that isn’t nearly as useful or eloquent. Nouns aren’t universal and verbs aren’t polymorphic. We’re throwing out decades of real field usage and proven technique and starting over with something that looks a lot like other systems that have failed in the past. We’re using HTTP but only because it helps us talk to our network and security people less. We’re trading simplicity for flashy tools and wizards.

davek
+4  A: 

One thing that jumps out at me (nice question by the way) is what they describe. The first describes an operation (getPart), the second describes a resource (part 00345).

Also, maybe you couldn't use other HTTP verbs with the first - you'd need a new method for putPart, for example. The second can be reused with different verbs (like PUT, DELETE, POST) to 'manipulate' the resource? I suppose you're also kinda saying GET twice - once with the verb, again in the method, so the second is more consistent with the intent of the HTTP protocol?

Brabster
Implicit in this answer is that REST is more than just having nice URLs. It's about using the HTTP protocol the way it was designed to be used, i.e. applying the logical verbs/methods to nouns/resources/URLs.
Matthew Crumley
A: 

"The second requires additional code and/or server configuration to parse out values,"

Really? You choose a poor framework, then. My experience is that the RESTful version is exactly the same amount of code. Maybe I just lucked into a cool framework.

"as well as additional documentation and time spent explaining the system to junior programmers"

Only once. After they get it, you shouldn't have to explain it again.

"and justifying it to peers."

Only once. After they get it, you shouldn't have to explain it again.

S.Lott
these are good comments and I agree with you.But I think you don't really answered the question. "my question is, aside from the pleasure of having URLs that look cool, what advantages do RESTful URLs gain for me that would make using them worth the cost of implementation?"
Diego Dias
@Diego Das: The "question" wasn't sensible. There are no costs of implementation. The fundamental premise was three things that appear to be baseless complaints.
S.Lott
Everything has a cost. Digging up the documentation for how to implement this feature in each of the several frameworks we use costs time. If I have to teach 10 developers the methods I've learned, and that meeting lasts 30 minutes, that costs my company $500. If I have to convince skeptical colleagues to do something the fancy new way, instead of whats tried and true, that costs me political capital. Before I go through all that, I want to know if and why this change is worth it.
Spike Williams
@Spike Williams: Compared to the long-term cost maintenance, enhancement, adaptation and operation, your "costs" are effectively zero. Compared with the "value" of a clean, simple, easy-to-understand interface, your costs are effectively zero. Learning something new (and changing your habits) is a benefit, not a cost. I cannot understand why minor things like talking with your colleagues even counts as a "cost". You **do** talk to them, don't you? How is talking about REST any different from any other conversation?
S.Lott
@Spike Williams: Assuming you're telling your skeptical colleagues about a better way to do things, I'd think you would gain political capital in the long run, rather than expend it.
GreenMatt
+1  A: 

The biggest advantage of REST IMO is that it allows a clean way to use the HTTP Verbs (which are the most important on REST services). Actually, using REST means you are using the HTTP protocol and its verbs.

Using your urls, and imagining you want to post a "part", instead of getting it

First case should be like this:

You are using a GET where you should have used a post

http://www.parts-depot.com/parts/postPart?param1=lalala&param2=lelele&param3=lilili

While on a REST context, it should be

http://www.parts-depot.com/parts

and on the body, (for example) a xml like this

<part>
   <param1>lalala<param1>
   <param2>lelele<param1>
   <param3>lilili<param1>
</part>
Diego Dias
I think this is the best explanation so far, although I'm still not convinced that the RESTful URL style is fixing something that was broken. Its still perfectly possible to use GET and POST on the same URL, regardless of whether you use the query sting in the GET request or if you store that data in the URL. Why is that solution deprecated in favor of RESTful URLs?Does it really just boil down to "clean" URLs being nice to have for their own sake? Or is there something I can do with REST URLs that just wouldn't work under a Plain Old HTTP Request paradigm?
Spike Williams
As I said on the answer (maybe I was not clear enough), REST intends to use HTTP Verbs as they were designed to...You can do anything with GET/POST. However, how should you update a resource which is idempotent without the PUT VERB? You can do it using a POST and even a GET, but if there is a VERB on the HTTP Protocol to do this, why don't use it? It should be cleaner, easier to understand and as you will be using the protocol anyway, use it as the protocol itself would expect ... :)
Diego Dias
There is no such thing as a REST URL.
serialseb
@serialseb You are right, I just edited the answer. Thx!
Diego Dias
+5  A: 

The hope is that if you make your URL refer to a noun then there is a better chance that you will implement the HTTP verbs correctly. Beyond that there is absolutely no advantage of one URL versus another.

The reality is that the contents of an URL are completely irrelevant to a RESTful system. It is simply an identifier.

It's not what it looks like, it is what you do with it that is important.

Darrel Miller
+1  A: 

URI semantics are defined by RFC 2396. The extracts particularly pertinent to this question are 3.3. "Path Component":

The path component contains data, specific to the authority (or the scheme if there is no authority component), identifying the resource within the scope of that scheme and authority.

And 3.4 "Query Component":

The query component is a string of information to be interpreted by the resource.

Note that the query component is not part of the resource identifier, it is merely information to be interpreted by the resource.

As such, the resource being identified by your first example is actually just /parts/getPart. If your intention is that the URL should identify one specific part resource then the first example does not do that, whereas the second one (/parts/00345) does.

So the 'advantage' of the second style of URL is that it is semantically correct, whereas the first one is not.

Greg Beech
That is a fascinating quote. I will have to dig into that more as I have seen Roy make statements on a few occasions that seem to be in direct contradiction to that.
Darrel Miller
Check out RFC3986 which replaces RFC2396 http://labs.apache.org/webarch/uri/rfc/rfc3986.html#query. In the Query section 3.4 there is the following quote *The query component contains non-hierarchical data that, along with data in the path component, serves to identify a resource within the scope of the URI's scheme*
Darrel Miller
Hmmm interesting... I hadn't realised that the semantics had been changed by the updated RFC. I read the original years ago; I just assumed the update wouldn't alter the semantics significantly. That'll teach me to be behind the times. OK so looks like in 2004 this answer would have been right, but now it's wrong :-S
Greg Beech
A: 

Don't use query/search parts in URLs which aren't queries or searches, if you do that - according to the URL spec - you are likely implying something about that resource that you don't really want to.

Use query parts for resources that are a subset of some bigger resource - pagination is a good example of where this could be applied.

Mike