无效数据的REST响应代码 [英] REST response code for invalid data

查看:254
本文介绍了无效数据的REST响应代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果出现以下情况,应将哪些响应代码传递给客户?

What response code should be passed to client in case of following scenarios?


  1. 用户注册时传递的数据无效,如错误的电子邮件格式

  2. 用户名/电子邮件已存在

我选择了403.我也发现了以下内容感觉可以使用。

I chose 403. I also found following that I feel can be used.


维基百科:

Wikipedia:

412前提条件失败:
服务器不符合请求者
对请求提出的前提条件之一

412 Precondition Failed : The server does not meet one of the preconditions that the requester put on the request

如果我应该建议代码除了403之外的其他用途。

Suggest code if I should use other than 403.

推荐答案

在这两种情况下,400是最佳选择。如果您想进一步澄清错误,您可以更改原因短语或包含正文以解释错误。

400 is the best choice in both cases. If you want to further clarify the error you can either change the Reason Phrase or include a body to explain the error.

412 - 使用前提条件失败用于条件请求最后修改日期和ETag。

412 - Precondition failed is used for conditional requests when using last-modified date and ETags.

403 - 当服务器希望阻止访问资源时使用Forbidden。

403 - Forbidden is used when the server wishes to prevent access to a resource.

唯一可能的选择是422 - 无法处理的实体。

The only other choice that is possible is 422 - Unprocessable entity.

这篇关于无效数据的REST响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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