什么时候响应 HTTP 412 错误是合适的? [英] When is it appropriate to respond with a HTTP 412 error?

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

问题描述

我不清楚什么时候应该和不应该返回 HTTP 412: Precondition Failed, error for a web service?我正在考虑在验证数据时使用它.例如,如果客户端 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)?

推荐答案

如果你看看 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天全站免登陆