What does using RESTful URLs buy me?

2019-03-11 21:40发布

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?

8条回答
【Aperson】
2楼-- · 2019-03-11 21:40

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.

查看更多
叼着烟拽天下
3楼-- · 2019-03-11 21:43

One way of looking at REST:

http://tomayko.com/writings/rest-to-my-wife (which has now been taken down, sadly, but can still be see on web.archive.org)

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.

查看更多
趁早两清
4楼-- · 2019-03-11 21:46

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.

查看更多
We Are One
5楼-- · 2019-03-11 21:58

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?

查看更多
混吃等死
6楼-- · 2019-03-11 22:01

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.

查看更多
迷人小祖宗
7楼-- · 2019-03-11 22:01

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.

查看更多
登录 后发表回答