80

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

2
  • Treat all 4YY errors as client errors. Treat 412 Precondition Failed as 400 Bad Request. That is, the client (GUI / script) has sent the wrong input to an API. Commented Apr 9, 2021 at 19:10
  • HTTP error 412 is used when you are using row versions or timestamps to avoid concurrency issues. If you sent the web service a record and include the row version in the header, the web service can compare that row version to the current row version. If the values don't match, you reject the request to update with a 412 error.
    – Quark Soup
    Commented Aug 17, 2021 at 11:17

4 Answers 4

96

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.

2
  • 1
    412 is also used when the row version (optimistic concurrency) fails. If you send a request with a row version that doesn't match the row version that the server has, you should fail with 412.
    – Quark Soup
    Commented Aug 17, 2021 at 11:15
  • 1
    @Quark Soup that is what If-Match is for. Although it is not the only way to do it. Commented Apr 25, 2023 at 8:11
27

412 is reserved for cases where the request is conditional, and the condition isn't met.

For your use case, 422 Unprocessable Entity is a good match.

4
  • 10
    It's worth mentioning that status code 422 is not part of RFC 2616. It's part of an extension to HTTP 1.1 for WebDAV. Commented Mar 20, 2011 at 18:58
  • 3
    joelittlejohn: why is that a problem? Status codes are an extension point in RFC 2616, and this is a very useful extension. Commented Jun 18, 2011 at 7:28
  • 6
    I didn't mean to imply that it's a problem, only that it's worth mentioning :) Commented Jun 29, 2011 at 23:40
  • I like this as well. It clearly distinguishes requests that are rejected for business validation reasons from those rejected due to transient conditions or software bugs. These three classes of error need to be very differently.
    – dsmith
    Commented Dec 17, 2018 at 17:20
11

Your best bet would be to avoid 412. In practice most web services that I've used send a 400 code (Bad Request). A lot of frameworks have built-in support for 400 too and your clients will appreciate a more common error code. Often times, especially with REST interfaces, a simple "message" or "error" element is returned with a description.

3
  • 5
    Clients are supposed to treat any unknown 4xx code as 400 anyway. Commented May 13, 2014 at 11:58
  • 1
    @JoelFan 404 wouldn't be an unknown code. That one should probably be handled while any 400-499 codes not explicitly handled should be treated as a generic 400.
    – BamaPookie
    Commented Jun 29, 2016 at 19:48
  • 1
    @BamaPookie - 404 is resource not found. 412 is precondition failed, which seems vague. Is that the reason why we prefer to treat all the vague responses as 400 ? Or is it something else ?
    – MasterJoe
    Commented Feb 15, 2017 at 7:12
0

This Problem occur due to following HTTP Header

  • if-none-match
  • if-modified-since

First check these HTTP header value for present request by following code

public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException
    {
         Enumeration<String> enumeration = httpServletRequest.getHeaderNames();
        
         while (enumeration.hasMoreElements()) 
         {
            String name = (String) enumeration.nextElement();
            System.out.println(name +" === "+httpServletRequest.getHeader(name));
    
         }
    }

That will look like following.

  • if-none-match === W/"1116-1693458399679" ,
  • if-modified-since === Thu, 31 Aug 2023 05:06:39 GMT

After getting these HTTP header value change these HTTP header value to handle request and handle 412 HTTP code Error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.