本文翻译自:REST HTTP status codes for failed validation or invalid duplicate
I'm building an application with a REST-based API and have come to the point where i'm specifying status codes for each requests. 我正在使用基于REST的API构建应用程序,并且到了我为每个请求指定状态代码的地步。
What status code should i send for requests failing validation or where a request is trying to add a duplicate in my database? 对于验证失败的请求,或者请求试图在数据库中添加重复项的情况,我应该发送什么状态代码?
I've looked through http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html but none of them seems right. 我已经浏览了http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html,但是似乎都不对。
Is there a common practice when sending status codes? 发送状态代码时是否有惯例?
#1楼
参考:https://stackoom.com/question/DnvS/验证失败或重复无效的REST-HTTP状态代码
#2楼
200,300, 400, 500 are all very generic. 200,300、400、500都是非常通用的。 If you want generic, 400 is OK. 如果要通用,则400即可。
422 is used by an increasing number of APIs, and is even used by Rails out of the box. 422被越来越多的API使用,甚至被Rails开箱即用。
No matter which status code you pick for your API, someon