REST HTTP状态代码,用于验证失败或重复无效 [英] REST HTTP status codes for failed validation or invalid duplicate

查看:242
本文介绍了REST HTTP状态代码,用于验证失败或重复无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于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?

我看了 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 但似乎没有一个对。

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?

推荐答案

输入验证失败: 400错误请求 +您的可选描述。这在 RESTful Web Services
双重提交: 409 Conflict

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月更新

以前的相关规范是 RFC2616 ,它使用了400(Bad Request)而不是狭义地

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


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

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

因此可能被认为不适合语义错误。但不是更多;自2014年6月起, RFC 7231 (取代之前的RFC2616)的相关标准允许使用 400(错误请求)更广泛地

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


服务器不能或
不会处理请求,因为有些东西被认为是
a客户端错误

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

这篇关于REST HTTP状态代码,用于验证失败或重复无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆