What is the HTTP error code for refusing to create a new resource because the parent is missing

Say I have an API that has two related resources, a company with many employees.

Let's say I create a new company: POST http: // domain / api / company / , which returns something like http: // domain / api / company / 123 .

If company / 123 is removed from the system (say, using DELETE), GET http: // domain / api / company / 123 can return an HTTP 410 (Gone) response code.

My question is that. If now I try to create Employee in company / 123 by doing POST http: // domain / api / employees / (with the companyId identifier set in the request block 123), what HTTP response code should the server send due to an invalid request?

eg. the request is correctly formed, but there is a logical error due to the fact that the company 123 has left.

500 Server Internal Error?

+4
source share
2 answers

Not 500 because there is no problem with the server.

I would suggest 409 Conflict.

From RFC:

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 will be able to resolve the conflict and resubmit the request. The response body MUST include sufficient information so that the user can recognize the source of the conflict. Ideally, the response object will contain sufficient information for the user or user agent to resolve the problem; however, this may be impossible and not required. Conflicts most often occur in response to a PUT request. For example, if version control was used, and the PUT object included changes to a resource that conflicts with those made using an earlier (third-party) request, the server can use a 409 response to indicate that it cannot fulfill the request. In this case, the response object is likely to contain a list of differences between the two versions in the format defined by the Content-Type response.

This does not exactly fit your case, but very close IMHO.

For example, the server may tell you that the parent resource does not exist for you, and you can "resend" an employee of another company. :)

+9
source

In this situation, I came across.

After evaluating the parameters of the HTTP status code, it seems to me the best option is to return 424 Failed Dependency

Status code 424 (Failed Dependency) means that the method may not execute on the resource because the requested action depended on another action and this action failed. For example, if a command in the PROPPATCH method fails, then at least the rest of the command will also fail with 424 (Failed Dependency).

From RFC

0
source

All Articles