Why do we need anything more than HTTP GET, PUT, P

2019-03-13 01:58发布

What is the practical benefit of using HTTP GET, PUT, DELETE, POST, HEAD? Why not focus on their behavioral benefits (safety and idempotency), forgetting their names, and use GET, PUT or POST depending on which behavior we want?

Why shouldn't we only use GET, PUT and POST (and drop HEAD, DELETE)?

标签: rest
14条回答
Luminary・发光体
2楼-- · 2019-03-13 02:31

To limit ambiguity which will allow for better/easier reuse of our simple REST apis.

查看更多
太酷不给撩
3楼-- · 2019-03-13 02:31

See the following link for an illustrative example. It also suggests one way to use the OPTIONS http method, which hasn't yet been discussed here.

查看更多
女痞
4楼-- · 2019-03-13 02:36

In a word:

idempotency

In a few more words:

GET = safe + idempotent

PUT = idempotent

DELETE = idempotent

POST = neither safe or idempotent

'Idempotent' just means you can do it over and over again and it will always do exactly the same thing.

You can reissue a PUT (update) or DELETE request as many times as you want and it will have the same effect every time, however the desired effect will modify a resource so it is not considered 'safe'.

A POST request should create a new resource with every request, meaning the effect will be different every time. Therefore POST is not considered safe or idempotent.

Methods like GET and HEAD are just read operations and are therefore considered 'safe' aswell as idempotent.

This is actually a pretty important concept because it provides a standard/consistent way to interpret HTTP transactions; this is particularly useful in a security context.

查看更多
劳资没心,怎么记你
5楼-- · 2019-03-13 02:37

Web applications using GET and POST allow users to create, view, modify and delete their data, but do so at a layer above the HTTP commands originally created for these purposes. One of the ideas behind REST is a return to the original intent of the design of the Web, whereby there are specific HTTP operations for each CRUD verb.

Also, the HEAD command can be used to improve the user experience for (potentially large) file downloads. You call HEAD to find out how large the response is going to be and then call GET to actually retrieve the content.

查看更多
霸刀☆藐视天下
6楼-- · 2019-03-13 02:38

There are http extensions like WebDAV that require additional functionally.

http://en.wikipedia.org/wiki/WebDAV

查看更多
祖国的老花朵
7楼-- · 2019-03-13 02:40

No one posted the kind of answer I was looking for so I will try to summarize the points myself.

"RESTful Web Services" chapter 8 section "Overloading POST" reads: "If you want to do without PUT and DELETE altogether, it’s entirely RESTful to expose safe operations on resources through GET, and all other operations through overloaded POST. Doing this violates my Resource-Oriented Architecture, but it conforms to the less restrictive rules of REST."

In short, replacing PUT/DELETE in favor of POST makes the API harder to read and PUT/DELETE calls are no longer idempotent.

查看更多
登录 后发表回答