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

4

79

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

Unsightly answered 20/3, 2011 at 15:40 Comment(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.Allegedly
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.Rader
I
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.

Immortal answered 20/3, 2011 at 15:57 Comment(2)
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.Rader
@Quark Soup that is what If-Match is for. Although it is not the only way to do it.Devora
P
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.

Predecessor answered 20/3, 2011 at 16:47 Comment(4)
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.Immortal
joelittlejohn: why is that a problem? Status codes are an extension point in RFC 2616, and this is a very useful extension.Predecessor
I didn't mean to imply that it's a problem, only that it's worth mentioning :)Immortal
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.Sere
P
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.

Puncture answered 20/3, 2011 at 15:46 Comment(3)
Clients are supposed to treat any unknown 4xx code as 400 anyway.Predecessor
@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.Oxygen
@Oxygen - 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 ?Heresiarch
C
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

Clergyman answered 27/9, 2023 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.