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

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

问题描述

我正在使用基于 REST 的 API 构建一个应用程序,并且已经到了为每个请求指定状态代码的地步.

I am building an application with a REST-based API and have come to the point where I am 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 服务"一书中建议.对于双重提交: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请求)相当狭隘地为

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 月起,取代之前的 RFC2616 的相关标准 RFC 7231 使用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

服务器不能或由于某些被认为是客户端错误

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天全站免登陆