Should RESTful api return 200or 404 if the data does not exist?

scenario: get user chat information: GET / messages?user=1
if the user does not have a chat history, should the data be returned with a null value of 200or 404 does not exist?

200
{
    "status": 200   -sharpHTTP
    "message": "ok"
    "data" : []
}
404
{
    "status":404    -sharpHTTP
    "message":""
    "error_code":1000
}

updated on 2018-12-4:
with reference to Douban and GitHub, it seems that both of them are useful
Douban: https://api.douban.com/v2/boo.
returns 404 when the book cannot be obtained

clipboard.png

GitHub:https://api.github.com/search...:Ruan Yifeng3

clipboard.png

Nov.24,2021

this varies according to the user's preference, some are constrained by the format of the response body, and some are constrained by the http status code + response body.

because you mentioned rest, there is no problem with using 404s. Rest has intended to associate http method and http status with resources. Other answers have mentioned that it is impossible to distinguish between interface level and resource level. The request with 404 status can also be sent back to body. What's wrong with that? 4xx means that Client Error, does not directly return error stack information. If it comes to user experience, it will affect some, but it can be improved by customizing the response page of 4xx.

there is no absolute error in using 200, it's just a habit. As far as I know, most of Tencent's interfaces are communicated according to the message format of 200. So take measures according to local conditions, see which one to use, but remember not to mix it up, it is easy to cause confusion.

another point is that it won't do any good if the front end always expects to return an interface with a state of 200 while ignoring error handling.


returns 200. Returns the internal code code and msg information of the convention in the content. Otherwise, how can you tell whether the calling interface does not exist or the requested resource does not exist? If you do not give 200, the frontend will go to the error or fail callback if you use Ajax, and the success callback will not be performed normally, and the frontend will chase you with a knife.


first look at Baidu-> 404 status code: the requested page does not exist or has been deleted!
according to Baidu, we can think about a question, what is the page? The
page must be rendered through the back-end data, which represents the resource.
so the page is the carrier of the resource.
what else are resources? Files, pictures, video, audio and even a string can represent resources. The
resource is certainly not a table, but real data.

Let's take a look at what restful-style api is: first of all, the uri of restful-style api represents resources, using verbs + objects, verbs represent GET (get) / POST (add) / PUT (full modification) / DELETE (delete) / PATCH (partial modification), etc., and the object is the resource. For example: POST / usrs: represents a new user, and GET / users: gets all users

and the status code represents the state of the result after we move the object.
404 represents in res*tful that the requested resource does not exist or is not available.
is it wrong that the api result of our rest returns 404 for null?
as for some people who say that the front end can't accept 404, I think that's the front end problem, not the back end problem.
others say that if it is encapsulated into a custom code, then what restful style api, do you use to directly customize code and message processing?
but at present, many users do not have a deep understanding of 404, and they feel that there is a problem as soon as they see it, so the back-end staff say they are very helpless and have to change the 404 to 200 at the back end, or go to the front end to deal with 404 specially. *


404 (not found), "not found" does not mean that the data is not found, but that the server cannot find the requested link, web page, or file.


it is best to return a null value of 200. Because if you pass 404, you can't tell whether the interface can't be found or the user doesn't have a chat record.


definitely return 200.You may mistakenly think that it is an interface problem at the front end of the 404.


it is recommended not to return 404, because exposing the status of HTTP to the user will have security problems, which is tantamount to telling the user what will happen to this request. You can all return 200. Or a unified internal definition, for example: return 10000 means password error


strictly speaking, it should be placed in HTTP code, but in practice it is still HTTP code 200, mostly in the message body.


200404 means that the interface does not exist


just like when you pick up mail, although there is no mail, the email address does exist. You can't use it


of course return 404, otherwise there is no restful

.
Menu