I'm working on an restful API back end, and we only use json as content type. And There are two ways of performing error response as I know:
I want to know which one is more idiom for a restful service?
You need to use both.
HTTP Status : Use this to process the status of the request. For example, if you query a DB and you find no entries, you'll still return 200. If the user is not authorized, 403, if number of SQL connections exceed, 500, and so on.
API Status : If the DB request succeed and you find no entries, include a custom field in your json {status: NO_ENTRIES }
or { status: DEPRECATED_API }
. In these cases, the response code will still be 200.
Generally speaking, I would not advocate for always returning a 200. There are failure scenarios that match commonly accepted status codes. Above, someone mentioned 403, which you deliver when access is denied. And 500 is typically issued by the web/app server when things really go south. And 404 if either a record is not found for a resource.
So, I do advocate for:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.