Ask HN: How do you use HTTP status codes in REST APIs?
I am curious as to which philosophy you use when designing a REST API that returns JSON.
If, for example, no record is found do you return a 404 status code or a 200 with an error message?
I'm partial to using HTTP status codes and was wondering what the HN consensus was.
5 comments
[ 3.6 ms ] story [ 22.4 ms ] threadthis policy is essential when bug hunting: if you get a 404, you should be certain its an http issue, not a db one
The answer is either way, and it depends on the tooling you are using. If you are relying on a client library that doesn't handle HTTP errors in the way you want, then you return a 200 - OK {"error": "bad request"}. If the client library does what you want, then returning a limited selection of 4xx errors is very useful for letting the client know what happened quickly and easily.
Personally, I default to using the HTTP error codes. But I know that some really large companies don't agree, and always return a 200 OK to any valid request.