尝试插入一个已经存在的对象时应该使用什么 HTTP 错误代码?

What HTTP error code should I use when trying to insert an already existing object?

我正在设计 REST API。此API的一些方法用于使用POST方法插入新数据。我想知道当插入会导致插入重复数据时 return 的错误代码是什么?我尝试查看错误代码参考,但我对 REST 和 HTTP 还很陌生,我不确定该使用哪一个。

409 Conflict 似乎适合这种情况。

W3C status code definitions document 说:

10.4.10 409 Conflict

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.