何时使用HTTP 412错误回应? [英] When is it appropriate to respond with a HTTP 412 error?

查看:1320
本文介绍了何时使用HTTP 412错误回应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您应该并且不应该返回HTTP 412时,我不清楚:前提条件失败,Web服务出错?我正在考虑在验证数据时使用它。例如,如果客户端POST的XML数据和该数据缺少必需的数据元素,则使用412和错误的描述进行响应。

It is unclear to me when you should and should not return a HTTP 412: Precondition Failed, error for a web service? I am thinking of using it when validating data. For example, if a client POST's XML data and that data is missing a required data element, then responding with a 412 and a description of the error.

是否与使用HTTP 412响应的精神,还是应该使用别的东西(例如另一个http错误代码或Web应用程序异常)?

Does that align with the spirit of responding with an HTTP 412, or should something else be used (e.g. another http error code or web application exception)?

推荐答案

p>如果您查看 RFC 2616 ,您将看到一些请求标题可以用于将条件应用于请求:

If you look at RFC 2616 you'll see a number of request headers that can be used to apply conditions to a request:

If-Match
If-Modified-Since
If-None-Match
If-Range
If-Unmodified-Since

这些标头包含'前提条件',允许客户端告诉服务器只有满足某些条件才能完成请求。例如,您使用 PUT 请求更新资源的状态,但您只需要 PUT 如果资源没有被其他人修改,因为您最近的 GET

These headers contain 'preconditions', allowing the client to tell the server to only complete the request if certain conditions are met. For example, you use a PUT request to update the state of a resource, but you only want the PUT to be actioned if the resource has not been modified by someone else since your most recent GET.

响应状态代码 412 (前提条件失败)通常在这些前提条件失败时使用。

The response status code 412 (Precondition Failed) is typically used when these preconditions fail.

您的示例听起来像一个无效的请求(即客户端提交的数据由于缺少值而无效)。状态码 400 (错误请求)在这里更适合IMO。

Your example sounds like an invalid request (i.e. the client has submitted data that is invalid because of missing values). A status code of 400 (Bad Request) is more appropriate here IMO.

这篇关于何时使用HTTP 412错误回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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