Http Status Code 400 vs 412

adamclerk picture adamclerk · May 24, 2012 · Viewed 25.5k times · Source

So I'm developing a Rest API

When a POST is made to create a resource and a required field is missing what should I return?

400 - Bad Request

OR

412 - Precondition Failed

And Why?

Answer

BalusC picture BalusC · May 24, 2012

Use 400 if the request parameters are wrong. Use 412 if one of the If-* request headers like If-Match, If-Modified-Since, etc are wrong.

Why? That's just what RFC says. See for example this extract of If-Match specification:

If none of the entity tags match, or if "*" is given and no current entity exists, the server MUST NOT perform the requested method, and MUST return a 412 (Precondition Failed) response. This behavior is most useful when the client wants to prevent an updating method, such as PUT, from modifying a resource that has changed since the client last retrieved it.