Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

http - 验证失败或重复无效的REST HTTP状态代码(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?

(发送状态代码时是否有惯例?)

  ask by alexn translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For input validation failure: 400 Bad Request + your optional description.

(对于输入验证失败: 400错误的请求 +您的可选描述。)

This is suggested in the book " RESTful Web Services ".

(在“ RESTful Web服务 ”一书中建议这样做。)

For double submit: 409 Conflict

(对于双重提交: 409冲突)


Update June 2014

(2014年6月更新)

The relevant specification used to be RFC2616 , which gave the use of 400 (Bad Request) rather narrowly as

(相关的规范曾经是RFC2616 ,它给出了400(错误请求)的使用范围,)

The request could not be understood by the server due to malformed syntax

(由于语法格式错误,服务器无法理解该请求)

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

(自2014年6月起,相关标准RFC 7231 (取代了先前的RFC2616 )在以下方面更广泛地使用了400(错误请求) :)

the server cannot or will not process the request due to something that is perceived to be a client error

(由于某些原因,服务器无法或将不会处理请求,因为这被视为客户端错误)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...