Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What HTTP error code for failure to create a new resource because a parent entity is gone

Tags:

rest

http

api

Say i have an API exposing two related resources, Company which has many Employees.

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 by a DELETE) then GET http://domain/api/company/123 could return HTTP response code 410 (Gone).

My question is this. If I now try to create an Employee under Company/123 by doing POST http://domain/api/employees/ (with companyId set to 123 in the request body) what HTTP response code should be sent back by the server due to the invalid request?

E.g. the request is correctly formated, but there is a logical error due to the fact that company 123 is gone.

Internal Server Error 500?

like image 430
andyc Avatar asked Aug 17 '11 05:08

andyc


People also ask

What is HTTP Error code1?

The server does not support, or refuses to support, the HTTP protocol version that was used in the request message. The server is indicating that it is unable or unwilling to complete the request using the same major version as the client, as described in section 3.1, other than with this error message.

What is a 207 error code?

207: Multi-Status The default message/response is a text/XML message. It indicates that multiple operations have taken place and that the status for each operation can be viewed in the body of the response.

What is the 409 response code?

The HTTP 409 status code (Conflict) indicates that the request could not be processed because of conflict in the request, such as the requested resource is not in the expected state, or the result of processing the request would create a conflict within the resource.

What does HTTP 201 response code specifies?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.


2 Answers

Not a 500, because there is no problem with the server.

I would suggest 409 Conflict.

From the 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 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. Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

It doesn't exactly match your case, but it is very close IMHO.

For example the server could tell you the parent resource does not exist for you to post to, and you can "resubmit" the employee to a different company. :)

like image 116
Ray Toal Avatar answered Oct 21 '22 02:10

Ray Toal


I ran in the same situation here.

After evaluating the HTTP Status code options, it appears to me the best option is to return a 424 Failed Dependency

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).

From RFC

like image 30
ricardorover Avatar answered Oct 21 '22 02:10

ricardorover