I'm designing a RESTful API that is using the HTTP status codes and verbs as key components in communicating.
On the religious level it's on the zealot side of RESTafarian.
Rule of thumb for deciding HTTP status codes has been this graph, or similar resources.
GET /api/documents/1
-401
User has not logged inGET /api/documents/1
-200
User has permissionGET /api/documents/1
-403
User does not have permissionDELETE /api/documents/1
-204
Users has permissionDELETE /api/documents/1
-403
User does not have permissionGET /api/documents/2
-404
Users permission irrelevant, resource does not existDELETE /api/documents/2
-404
Users permission irrelevant, resource does not existDELETE /api/documents/1
-404
Users has permission, resource already deletedDELETE /api/documents/1
-404
Users does not have permission, resource already deleted
Goals:
- Consistency in usage
- Not to expose private information through errors
- Proper use of status codes for client or middle layer caches
- Fail early, keep lookups to a minimum
In this situation there is a lot of different status code to chose from ( 404, 403, 410, 405) and in my case I went with 403 on a existing resource if its not yours to not clear the cache, and 404 on all non existing resources so to tell the clients to wipe that data.
But I do not like the switch from 403 to 404 on resources that are not yours.
I'm interested to hear from others how you solved this use-case, or in general what status codes you feel appropriate to send in all invalid DELETE calls, since I deem that as one of the hardest to be concise with.
(A lot of REST discussions and answers on the internet in it whole are just "Throw a 400 bad request, no one cares anyway", I do not have a problem that needs a quick fix or a pragmatic hack. Thanks)