Reputation: 21599
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?
Upvotes: 8
Views: 11094
Reputation: 3765
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.
Upvotes: 14
Reputation: 42045
424 is defined in WebDAV; this is not a problem, but WebDAV uses for the error situation that you describe status code 409 (conflict). That's what I would use.
Upvotes: 0
Reputation: 47965
IMHO you should, the question is more how well known this status code. I had to look into Wikipedia first.
However I would say that even if the client does not handle this code by default, it is quite easy to guess that this is a client error.
If possible I would add a hint what call was missing.
Upvotes: 0