什么是最好的 Http 代码来识别幂等操作冲突

what is the best Http code to identify idempotent operation conflict

我开发了 res 服务,它在每次调用时都采用唯一的 id 参数,但是当同一 id 多次使用时,它应该检索相同的响应,这是第一次检索并且状态代码指定错误,我正在寻找最好的状态代码,一些 post 使用“409 Conflict”,一些“406 Not Acceptable”,使用哪个?

409更好,因为406多用于表示header not acceptable

406 Not Acceptable The requested resource is capable of generating only content not acceptable according to the Accept headers sent in the request.

409(冲突)表示您的请求重复。

409 Conflict Indicates that the request could not be processed because of conflict in the request, such as an edit conflict between multiple simultaneous updates.

我想对 table 发表不同的看法。 “为什么我们要 return 409 总是打扰客户来处理这些情况?为什么客户要为项目是作为当前请求的一部分还是过去的尝试之一创建而烦恼?”

我觉得对于像“POST on collection to create a resource instance”这样简单的场景,如果server returns 201 即使在幂等响应的情况下,客户端也可以被简化。

但是,我们还应该确保这种简化不会在某些情况下造成混淆,例如: 资源是根据来自客户端的 POST 请求使用初始版本 V1 创建的。然后该资源得到更新并移动到版本 V2。现在出于某种原因,初始客户端使用相同的幂等性令牌重试 POST 请求。 我认为后一个应该 returned 为 409,因为这是冲突。

POST 预计将使用请求中存在的数据创建资源的初始版本。由于这是重复请求,我们宁愿将现有资源实例和 return 获取到客户端。该资源状态可能没有创建请求中指定的所有字段,因此存在冲突。