Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to respond with a HTTP 412 error?

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.

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)?

like image 259
TERACytE Avatar asked Mar 20 '11 15:03

TERACytE


People also ask

What is a 412 HTTP response code?

The HyperText Transfer Protocol (HTTP) 412 Precondition Failed client error response code indicates that access to the target resource has been denied.

How do I fix Error 412?

Note : If you have a WordPress based website hosted over the server and have Bad Behavior plugin installed which is active, then you must first try deactivating it. This plugin is a PHP-based solution for blocking link spam and the robots which deliver it, which usually causes such an error in WordPress.


1 Answers

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 

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.

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

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.

like image 79
joelittlejohn Avatar answered Sep 30 '22 17:09

joelittlejohn