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

查看:29
本文介绍了无效数据的 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.

维基百科:

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 - Precondition failed 用于使用上次修改日期和 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天全站免登陆