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条回答
smile是对你的礼貌
2楼-- · 2019-03-11 22:04

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>
查看更多
够拽才男人
3楼-- · 2019-03-11 22:05

"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.

查看更多
登录 后发表回答