4

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?

3
  • 1
    here's a guide for choosing the appropriate HTTP Status Code Commented Sep 24, 2016 at 8:49
  • @AlexandruMarculescu Nice read. Thank you Commented Sep 24, 2016 at 10:56
  • @AlexandruMarculescu, The link you provided no longer exists. Commented Sep 17, 2020 at 1:31

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

Thanks John, Your explanation for 404 and 410 seem very right on. Per w3, The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource. . The DELETE method is valid, but just not all the time. Will the client give up if they get a 405? I am also considering 422, but not sure.
409 is probably more common for data dependency conflicts. 409 = conflict =)
1

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.

3 Comments

Thanks Alice, 304 can't respond with a message, so maybe not? Maybe 412? 400 wouldn't be good because the condition which made it a bad request might no longer be present. There is also tools.ietf.org/html/rfc4918 which gives more. Would be nice to be more defined.
304 - Not Modified - is feasible if the ingredient had already been deleted by a prior request or if the ingredient no longer exists for some other reason. However, neither case applies for this situation.
304 is part of the Redirection group, and not appropriate in this case, which is a client error so the code needs to be in the 4xx group. 304 is used to tell the client that the resource hasn't been modified since the last time they visited so the cached local copy the client has is still current.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.