在HTTP DELETE方面查看幂等性的正确方法是什么? [英] What's the correct way to view idempotency in terms of HTTP DELETE?

查看:115
本文介绍了在HTTP DELETE方面查看幂等性的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近花了很多时间阅读HTTP 1.1规范并将其与REST相关联。我发现HTTP DELETE方法在其幂等性和安全性方面有两种解释。以下是两个阵营:

I have spent a lot of time recently reading the HTTP 1.1 specification and relating it to REST. I have found that there are two interpretations of the HTTP DELETE method in regards to its "idempotency" and safety. Here are the two camps:


  1. 如果使用HTTP DELETE删除资源,并且成功(200 OK),然后如果您尝试删除该资源N次,则应该为每个删除调用取回成功消息(200 OK)。这是它的幂等性。

  1. If you delete a resource with HTTP DELETE, and it succeeds (200 OK), and then you try to delete that resource N number of times, you should get back a success message (200 OK) for each and every one of those delete calls. This is its "idempotencyness".

如果使用HTTP DELETE删除资源,并且成功(200 OK),然后尝试删除该资源再次,您应该收到一条错误消息(410 Gone),因为资源已被删除。

If you delete a resource with HTTP DELETE, and it succeeds (200 OK), and then you try to delete that resource again, you should get back an error message (410 Gone) because the resource was deleted.

规范说DELETE肯定是幂等的,但它也说幂等事件的序列仍然会产生副作用。我真的觉得第二阵营是正确的,第一阵营是误导。我们通过允许客户认为他们是删除以前删除的资源的原因引入了什么安全?

The specification says DELETE is idempotent, sure, but it also says that sequences of idempotent events can still produce side effects. I really feel like the second camp is correct, and the first is misleading. What "safety" have we introduced by allowing clients to think they were the cause for deleting a resource previously deleted?

第一阵营中有很多人,包括关于这个问题的几位作者,所以我想检查是否有一些令人信服的理由,而不是导致人们进入第一阵营的情绪。

There are a LOT of people in the first camp, including several authors on the subject, so I wanted to check if there was some compelling reason other than emotions that lead people into the first camp.

推荐答案

作为幂等并不意味着不允许请求产生副作用(这就是'安全'属性所描述的)。这只是意味着多次发出相同的请求不会导致不同或额外的副作用。

Being idempotent does not mean that a request is not allowed to have side-effects (that's what the 'safe' property describes). It just mean that issuing the same request multiple times will not result in different or additional side-effects.

在我看来,后续DELETE请求应该返回错误 - 它是仍然是幂等的,因为服务器的状态与只发出一个DELETE请求的状态相同。然后再次返回200 OK状态也应该没问题 - 我不认为是幂等需要为后续DELETE请求返回错误代码 - 这只是返回错误状态似乎使对我来说更有意义。

In my opinion, the subsequent DELETE request should return an error - it's still idempotent because the state of the server is that same as if only one DELETE request were made. Then again returning the 200 OK status should be OK as well - I don't think being idempotent requires the returning of an error code for the subsequent DELETE requests - it's just that returning the error status seems to make more sense to me.

这篇关于在HTTP DELETE方面查看幂等性的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆