第二次使用 HTTP DELETE 删除资源时的状态码 [英] Status code when deleting a resource using HTTP DELETE for the second time

查看:64
本文介绍了第二次使用 HTTP DELETE 删除资源时的状态码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于 HTTP 中的 DELETE 动词是幂等的,当我发出以下请求时,我第二次(或第三次,或第四次,等等)请求会发生什么?

Given that the DELETE verb in HTTP is idempotent, when I issue the following request, what should happen the second (or third, or fourth, etc...) time I make it?

DELETE /person/123

第一次删除资源,返回204(成功,无内容).我应该在后续调用中返回 204 还是 404(未找到)?

The first time, the resource is deleted and I return a 204 (successful, no content). Should I return a 204 on subsequent calls or a 404 (not found)?

推荐答案

由于无状态系统中的 HTTP 请求应该是独立的,因此一个请求的结果不应依赖于前一个请求.考虑如果两个用户同时对同一资源执行 DELETE 会发生什么.第二个请求得到 404 是有意义的.如果一个用户发出两个请求,同样应该如此.

As HTTP requests in a stateless system should be independent, the results of one request should not be dependent on a previous request. Consider what should happen if two users did a DELETE on the same resource simultaneously. It makes sense for the second request to get a 404. The same should be true if one user makes two requests.

我猜让 DELETE 返回两个不同的响应对您来说并不是幂等的.我发现将幂等请求视为使系统处于相同状态而不一定具有相同响应是有用的.因此,无论您是删除现有资源,还是尝试删除不存在的资源,服务器资源状态都是相同的.

I am guessing that having DELETE return two different responses does not feel idempotent to you. I find it useful to think of idempotent requests as leaving the system in the same state, not necessarily having the same response. So regardless of whether you DELETE an existing resource, or attempt to DELETE a resource that does not exist, the server resource state is the same.

这篇关于第二次使用 HTTP DELETE 删除资源时的状态码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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