user1032531
user1032531

Reputation: 26321

HTTP status code for inability to process request

Making a delete request to /api/ingredients/123 endpoint.

If ingredient 123 didn't exist, I expect I should return a 404 Not Found status code. Agree?

What if ingredient 123 did exist, but was used in an existing recipe so it couldn't be deleted. What status code should be returned?

Upvotes: 4

Views: 7419

Answers (3)

Saikrishna Radarapu
Saikrishna Radarapu

Reputation: 397

409 Conflict

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.

In your case, an ingredient cannot be deleted as it is used (conflicting state of the resource) in one or more recipes. But once the ingredient is out of use it can be deleted.

Upvotes: 4

JohnH
JohnH

Reputation: 2133

Re: HTTP status code in response to the condition that ingredient 123 didn't exist [and never did exist]; yes, I agree that the 404 - Not Found - is correct.

If ingredient 123 did exist and is now deleted, then 410 - Gone - is appropriate. (My website uses the 410 status code for obsolete products.)

However, your case has ingredient 123 existing, but that the delete request for that specific ingredient cannot be processed due to a data dependency conflict ("an existing recipe"). For this case, 405 - Method Not Allowed - is appropriate.

HTTP status of 405 indicates that the ingredient reference is OK, but not the delete request.

Wikipedia and w3.org have helpful pages on HTTP status codes.

Upvotes: 2

Alice Zakhary
Alice Zakhary

Reputation: 23

304 Not Modified, perhaps? Or, 412 Precondition Failed.

There's a list of status code definitions here: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

I think any answer to this question would be 30% technical, 70% opinion.

Upvotes: 1

Related Questions