问题描述
我正在使用基于REST的API构建一个应用程序,并且我已经为每个请求指定了状态代码。
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.
什么状态代码我应该发送未通过验证的请求或请求试图在我的数据库中添加副本的地方吗?
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.
发送状态代码有通用做法吗?
Is there a common practice when sending status codes?
推荐答案
输入验证失败: +您的可选描述。这在。
双重提交:
For input validation failure: 400 Bad Request + your optional description. This is suggested in the book "RESTful Web Services".For double submit: 409 Conflict
2014年6月更新
以前的相关规范是,它使用了400(Bad Request)而不是狭义地
The relevant specification used to be RFC2616, which gave the use of 400 (Bad Request) rather narrowly as
因此可能被认为不适合语义错误。但不是更多;自2014年6月起,(取代之前的RFC2616)的相关标准允许使用更广泛地
So it might have been argued that it was inappropriate for semantic errors. But not any more; since June 2014 the relevant standard RFC 7231, which supersedes the previous RFC2616, gives the use of 400 (Bad Request) more broadly as
这篇关于REST HTTP状态代码,用于验证失败或重复无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!