简体   繁体   English

Http状态代码400与412

[英]Http Status Code 400 vs 412

So I'm developing a Rest API 所以我正在开发一个Rest API

When a POST is made to create a resource and a required field is missing what should I return? 当POST创建资源并且缺少必填字段时我应该返回什么?

400 - Bad Request 400 - 错误请求

OR 要么

412 - Precondition Failed 412 - 前提条件失败

And Why? 为什么?

Use 400 if the request parameters are wrong. 如果请求参数错误,请使用400。 Use 412 if one of the If-* request headers like If-Match , If-Modified-Since , etc are wrong. 如果其中一个If-*请求标头(If-MatchIf-Modified-Since等)错误,请使用412。

Why? 为什么? That's just what RFC says. 这正是RFC所说的。 See for example this extract of If-Match specification: 例如,参见If-Match规范的这个摘录:

If none of the entity tags match, or if "*" is given and no current entity exists, the server MUST NOT perform the requested method, and MUST return a 412 (Precondition Failed) response. 如果没有实体标签匹配,或者如果给出“*”并且不存在当前实体,则服务器必须不执行所请求的方法,并且必须返回412(Precondition Failed)响应。 This behavior is most useful when the client wants to prevent an updating method, such as PUT, from modifying a resource that has changed since the client last retrieved it. 当客户端想要阻止更新方法(例如PUT)修改自客户端上次检索它以来已更改的资源时,此行为最有用。

412 is used when your server does not meet a condition specified by the client. 当您的服务器不满足客户端指定的条件时,将使用412。

In your case you should use a 400. It is just a bad request. 在你的情况下你应该使用400.这只是一个糟糕的要求。

See this link for some explaination on pre-condition headers. 有关前置条件标题的一些说明,请参阅此链接

The Etag header is, generally, a string that represents our resource in the HTTP headers. 通常,Etag标头是表示HTTP标头中的资源的字符串。 You ask for a resource with an If-Match is a preconditional HTTP header. 您要求具有If-Match的资源是前置HTTP标头。 It will send a 412 if it does not match the code you sent. 如果它与您发送的代码不匹配,它将发送412。

If-None-Match tells the server to process a whole response only if the Etag is different from the one sent by the client. If-None-Match告诉服务器仅在Etag与客户端发送的Etag不同时才处理整个响应。

您可以使用状态代码422.如果您不想,400就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM