DELETE应该是幂等的。
如果我删除http://example.com/account/123,它将删除帐户。
如果我再次这样做,我将期望404,因为帐户不再存在?如果我试图删除一个从未存在过的帐户怎么办?
DELETE应该是幂等的。
如果我删除http://example.com/account/123,它将删除帐户。
如果我再次这样做,我将期望404,因为帐户不再存在?如果我试图删除一个从未存在过的帐户怎么办?
当前回答
幂等性指的是请求完成后系统的状态
在所有情况下(除了错误问题-见下文),该帐户不再存在。
从这里
方法也可以具有的属性 “幂等性”(除了 错误或过期问题) 副作用N > 0相同 请求与单个请求相同 请求。方法GET, HEAD, PUT 和DELETE共享此属性。同时, 方法OPTIONS和TRACE应该 没有副作用,所以 本质上是等幂的。”
关键是N个>个相同请求的副作用与单个请求相同。
您期望状态码会有所不同是正确的,但这并不影响等幂的核心概念——您可以多次发送请求,而无需对服务器的状态进行额外更改。
其他回答
引用自我对另一个问题的回答:
Historically, RFC 2616, published at 1999, was the most-referenced HTTP 1.1 specs. Unfortunately its description on idempotency was vague, that leaves room for all these debates. But that specs has been superseded by RFC 7231. Quoted from RFC 7231, section 4.2.2 Idempotent Methods, emphasis mine: A request method is considered "idempotent" if the intended EFFECT ON THE SERVER of multiple identical requests with that method is the same as the effect for a single such request. Of the request methods defined by this specification, PUT, DELETE, and safe request methods are idempotent. So, it is written in the specs, idempotency is all about the effect on the server. The first DELETE returning a 204 and then subsequent DELETE returning 404, such different status code does NOT make the DELETE non-idempotent. Using this argument to justify a subsequent 204 return, is simply irrelevant. OK so it is not about idempotency. But then a follow-up question may be, what if we still choose to use 204 in subsequent DELETE? Is it OK? Good question. The motivation is understandable: to allow the client to still reach its intended outcome, without worrying about error handling. I would say, returning 204 in subsequent DELETE, is a largely harmless server-side "white lie", which the client-side won't immediately tell a difference. That's why there are people doing that in the wild and it still works. Just keep in mind that, such lie can be considered semantically weird, because "GET /non-exist" returns 404 but "DELETE /non-exist" gives 204, at that point the client would figure out your service does not fully comply with section 6.5.4 404 Not Found.
But then, the intended way hinted at by RFC 7231, i.e. returning 404 on a subsequent DELETE, shouldn't be an issue in the first place. Many more developers choose to do that. That is presumably because, any client which implements HTTP DELETE (or any HTTP method, for that matter), would not blindly assume the result would always be successful 2xx. And then, once the developer starts to consider the error handling, 404 Not Found would be one of the first errors that comes to mind. At that point, he/she would hopefully draw a conclusion that it is semantically safe for an HTTP DELETE operation to ignore a 404 error. Problem solved.
我也是这么想的,404 -账户不存在。
你可以争辩400 -坏请求。但是在REST的意义上,您请求执行操作的对象并不存在。这意味着404。
是的。不管响应代码是什么。
来自HTTP 1.1的最新RFC:
Idempotent methods are distinguished because the request can be repeated automatically if a communication failure occurs before the client is able to read the server's response. For example, if a client sends a PUT request and the underlying connection is closed before any response is received, then the client can establish a new connection and retry the idempotent request. It knows that repeating the request will have the same intended effect, even if the original request succeeded, though the response might differ.
It explicitly says that the response might differ. More importantly, it points out the reason of the concept: if an action is idempotent, the client can repeat the action when it encounters any error, and knows that it won't crash anything by doing so; if not, the client will have to make an additional query (possibly GET) to see whether the previous one is effective, before it safely repeat the action. As long as the server can make such guarantee, the action is idempotent. Quote from another comment:
计算幂等关系到系统的鲁棒性。由于事情可能会失败(例如网络中断),当检测到故障时,如何恢复?最简单的方法就是再做一遍,但只有在幂等的情况下才有效。例如,discard(x)是幂等的,但pop()不是。这都是关于错误恢复的。
来自HTTP RFC:
方法还可以具有“幂等性”属性,即(除了错误或过期问题外)N > 0个相同请求的副作用与单个请求相同。
注意这里是“side effects”,而不是“response”。
假设我们必须管理以id、名称和城市表示的足球队。
{
id: "1",
name: "manchester united",
city : "manchester "
}
说DELETE是幂等的意味着如果多次调用DELETE /team/1,系统的状态保持不变(实际上第一次调用DELETE /team/1会删除团队)。换句话说,DELETE是幂等的,因为重复调用保持系统状态不变。
同样地,我们可以说PUT也是等幂的。 假设你做了不止一次PUT:
PUT /team/1
{
id: "1",
name: "liverpool",
city : "liverpool"
}
这种PUT请求的重复调用总是具有相同的效果(球队1将是利物浦)。
很明显,GET请求也是幂等的。