Http Status Code 400 vs 412
Solution 1
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.
Solution 2
412 is used when your server does not meet a condition specified by the client.
In your case you should use a 400. It is just a bad request.
See this link for some explaination on pre-condition headers.
The Etag header is, generally, a string that represents our resource in the HTTP headers. You ask for a resource with an If-Match is a preconditional HTTP header. It will send a 412 if it does not match the code you sent.
If-None-Match tells the server to process a whole response only if the Etag is different from the one sent by the client.
Solution 3
You could use status code 422. If you don't want to, 400 is fine.
adamclerk
I'm a Senior Engineering Manager focusing on team and product transformation. I focus on deliveries in danger and their recovery and have a strong history of excellent performance. I pride myself in building strong engineering teams through honesty, integrity and hard work. My current expertise is in modern web development with an emphasis on service oriented architectures (REST & GraphQL) and technical frontends (SPA & PWA). I really enjoy working with a variety of peoples to create great software and experiences. My past work has included: - building and integrating microservices and technical frontend architectures for fortune 50 e-commerce companies - full stack development of systems for uploading, editing, storage and playback of online video - integrating with 3rd party rest api across multiple services ranging from PAAS, IAAS and SAAS - creating and maintaining efficient CI processes to maintain quality - creating and maintaining billion record etl processes Technical Expertise: - Backend: golang, node.js - Frontend: angular, react, html5, css3 - Cloud: AWS, Azure, PCF, OnPrem
Updated on March 21, 2020Comments
-
adamclerk about 4 years
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?
-
Rob almost 12 yearsIt also states the request could not be fulfilled due to malformed syntax for 400.
-
BalusC almost 12 yearsThink someone had a bad day. Just ignore it and let the system do its job :)
-
Charlie over 9 yearsThis page really helped me - odino.org/… - Using 'PUT' requests for context made it easier for me to understand what it's for, as it's always easy to abuse certain status codes.