When to use HTTP status code 404 in an API
Posted
by
Sybiam
on Programmers
See other posts from Programmers
or by Sybiam
Published on 2013-07-02T23:14:10Z
Indexed on
2013/07/03
11:16 UTC
Read the original article
Hit count: 411
I am working on a project and after arguing with people at work for about more than a hour. I decided to know what people on stack-exchange might say.
We're writing an API for a system, there is a query that should return a tree of Organization or a tree of Goals.
The tree of Organization is the organization in which the user is present, In other words, this tree should always exists. In the organization, a tree of goal should be always present. (that's where the argument started). In case where the tree doesn't exist, my co-worker decided that it would be right to answer response with status code 200. And then started asking me to fix my code because the application was falling apart when there is no tree.
I'll try to spare flames and fury.
I suggested to raise a 404 error when there is no tree. It would at least let me know that something is wrong. When using 200, I have to add special check to my response in the success callback to handle errors. I'm expecting to receive an object, but I may actually receive an empty response because nothing is found. It sounds totally fair to mark the response as a 404. And then war started and I got the message that I didn't understand HTTP status code schema. So I'm here and asking what's wrong with 404 in this case? I even got the argument "It found nothing, so it's right to return 200". I believe that it's wrong since the tree should be always present. If we found nothing and we are expecting something, it should be a 404.
More info,
I forgot to add the urls that are fetched.
Organizations
/OrgTree/Get
Goals
/GoalTree/GetByDate?versionDate=...
/GoalTree/GetById?versionId=...
My mistake, both parameters are required. If any versionDate that can be parsed to a date is provided, it will return the closes revision. If you enter something in the past, it will return the first revision. If by Id with a id that doesn't exists, I suspect it's going to return an empty response with 200.
Extra
Also, I believe the best answer to the problem is to create default objects when organizations are created, having no tree shouldn't be a valid case and should be seen as an undefined behavior. There is no way an account can be used without both trees. For that reasons, they should be always present.
also I got linked this (one similar but I can't find it)
http://viswaug.files.wordpress.com/2008/11/http-headers-status1.png
© Programmers or respective owner