For example I have a hierarchy of entities, and due to some issue I receive a request to create a child before the request to create a parent.
Is it correct to use code 424 in this case?
The 424 (Failed Dependency) status code means that the method could not be performed on the resource because the requested action depended on another action and that action failed.
HTTP status codes and the error message can give you a clue. In general, a 5xx status code can be retried, a 4xx status code should be checked first, and a 3xx or 2xx code does not need retried.
Informational responses ( 100 – 199 ) Successful responses ( 200 – 299 ) Redirection messages ( 300 – 399 ) Client error responses ( 400 – 499 )
The HTTP 201 status code is used when the server successfully responds to the request from the client and creates a suitable new resource for the request. It is used when the request sent by the client to the server is accepted by the server, but the request is still being processed on the server side.
There's definitely room for some opinions on this. RFC4918 defines HTTP status code 424 as (emphasis mine):
The 424 (Failed Dependency) status code means that the method could not be performed on the resource because the requested action depended on another action and that action failed. For example, if a command in a PROPPATCH method fails, then, at minimum, the rest of the commands will also fail with 424 (Failed Dependency).
In my opinion this does not fit your case as the dependent action has not yet happened, while this status code appears to indicate that the dependent action was received but failed.
Julian Rechke's answer of 409 Conflict is defined in RFC2616 as
The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.
However you should only use this response if the client receiving this response is able to fix the problem (in this case, creating the parent). If this is not the case, I might recommend a 422 Unprocessable Entity, again from RFC4918:
The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.
Or else just a good old-fashioned 400 Bad Request as it doesn't really make sense to create a child of a parent that does not exist, at the point in time of the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With