どうやら、RESTはHTTPの使い方に関する規約の集合に過ぎないこれらの規則にはどのような利点があるのでしょうか。誰か知っていますか?
ベストアンサー1
これに対する良い答えは得られないと思います。RESTが何なのかについて誰も同意していないからです。は。ウィキペディアのページは流行語が多く、説明が不足しています。議論ページは、人々がこれについてどれだけ意見が一致していないかを知るためにざっと目を通す価値があります。しかし、私が知る限り、REST とは次のような意味です。
ランダムに名前を付けたセッターとゲッターのURLを用意して、GET
すべてのゲッターに 、POST
すべてのセッターに を使用する代わりに、URLでリソースを識別し、HTTPアクションGET
、POST
、を使用しPUT
てDELETE
それらに対して操作を実行します。
GET /get_article?id=1
POST /delete_article id=1
あなたはそうするだろう
GET /articles/1/
DELETE /articles/1/
そして、 と はPOST
「PUT
作成」および「更新」操作に対応します (ただし、どちらの順序であるかについては誰も同意しません)。
キャッシュ引数が間違っていると思います。クエリ文字列がは一般的にキャッシュされ、実際に使用する必要はありません。たとえば、Django では次のようなことが非常に簡単にできますが、REST とは言えません。
GET /get_article/1/
POST /delete_article/ id=1
または、URL に動詞だけを含めることもできます。
GET /read/article/1/
POST /delete/article/1/
POST /update/article/1/
POST /create/article/
その場合、GET
副作用のない何か、つまりPOST
サーバー上のデータを変更する何かを意味します。これはおそらく、PUT
-vs-全体を回避できるため、少し明確で簡単だと思いますPOST
。さらに、必要に応じて動詞を追加できるため、HTTP が提供するものに人為的に縛られることはありません。たとえば、次のようになります。
POST /hide/article/1/
POST /show/article/1/
(とにかく、実際に起こるまで例を考えるのは難しいです!)
結論として、私が見つけた利点は 2 つだけです。
- Web API がよりわかりやすくなり、理解しやすくなり、見つけやすくなります。
- When synchronising data with a website, it is probably easier to use REST because you can just say
synchronize("/articles/1/")
or whatever. This depends heavily on your code.
However I think there are some pretty big disadvantages:
- Not all actions easily map to CRUD (create, read/retrieve, update, delete). You may not even be dealing with object type resources.
- It's extra effort for dubious benefits.
- Confusion as to which way round
PUT
andPOST
are. In English they mean similar things ("I'm going to put/post a notice on the wall.").
So in conclusion I would say: unless you really want to go to the extra effort, or if your service maps really well to CRUD operations, save REST for the second version of your API.
I just came across another problem with REST: It's not easy to do more than one thing in one request or specify which parts of a compound object you want to get. This is especially important on mobile where round-trip-time can be significant and connections are unreliable. For example, suppose you are getting posts on a facebook timeline. The "pure" REST way would be something like
GET /timeline_posts // Returns a list of post IDs.
GET /timeline_posts/1/ // Returns a list of message IDs in the post.
GET /timeline_posts/2/
GET /timeline_posts/3/
GET /message/10/
GET /message/11/
....
Which is kind of ridiculous. Facebook's API is pretty great IMO, so let's see what they do:
By default, most object properties are returned when you make a query. You can choose the fields (or connections) you want returned with the "fields" query parameter. For example, this URL will only return the id, name, and picture of Ben: https://graph.facebook.com/bgolub?fields=id,name,picture
I have no idea how you'd do something like that with REST, and if you did whether it would still count as REST. I would certainly ignore anyone who tries to tell you that you shouldn't do that though (especially if the reason is "because it isn't REST")!