How to (and how not to) design REST APIs
github.com
github.com
The author says it's ambiguous, because it could mean the route is not found, or it could mean the requested item is not found.
Some people use 204 to indicate the latter:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204
It basically means, "We received and understood your request, but you're not getting a response." And you can infer that it's because the content you requested is not there.
At a previous company, we returned a 200 for anything that was not a true HTTP error on our side, with a dedicated error message in the response for anything else. So, for instance, instead of returning a 403 when the API user provided invalid data, we would return a 200, indicating that we successfully did everything we were supposed to do, but then explained in the response what invalid data needed to be corrected.
I liked that approach. Anything non-200-level meant that we screwed up. And any 200-level response with an error message meant that the user screwed up.
As an alternative to using HTTP 404 for detecting a misconfigured server with bad routing, you could use the "Server" response header to include an invariant indicator as a confidence sign to the client that the HTTP 404 response does not mean bad-routing or misconfigured server - or expired-domain-name-now-points-to-a-domain-squatting-website.
[1] https://developer.mozilla.org/en-US/docs/Web/API/Navigator/s...
The author at least suggests using 400, which at the very least will make clients and proxies behave in a predictable way, but ideally you just return the appropriate HTTP status code, and use a well-defined response body to indicate why the specific error was returned.
I work at a company that does this and it is beyond annoying. We've had multiple discussions between the dev team and we chalk it up the difference between application developers, who want the error codes to reflect the state of the application, and network engineers, who want the error codes to reflect the state of the network. Both sides have merit, but as an app developer I really hate the networking engineer approach to this.
Seriously, stop reinventing what is already an established standard. There is nothing more annoying than a server that returns a 200-class status when the thing I requested is not in the response.
All application errors are returned with a 409 code, and then you are sure to have a properly json formatted error in the body that can will have an error code more relevant for your case.
So that could be NOT_FOUND in such a case.
I took it to mean 404 should mean you haven't hit the API at all.
I kind of get this and wouldn't complain about an API doing this, but I also wouldn't be surprised for `/api/v1/posts/abc-123` to return 404 because the route handler couldn't match the post ID.
I strongly disagree with this stance as I don't think 404 is ambiguous at all. Although it would be useful to know at what point in the broader hierarchy of a specific resource requested the unavailability starts: Ultimately 404 just means the specific 'tip of the spear' resource requested is not found.
Got 404, returned body is json, returned body is an error object, good to go.
Oh and, by returning a 404 you help intermediate caches know that they should not cache the response.
I disagree, as you then have to deal with English pluralization rules to match single objects and collections.
Also database tables should just be singular, things like 'country.id' makes more sense than 'countries.id'.
Also no discussion of versioning?
get /ticket/12
patch /ticket/12
put /ticket
The problem is not humans getting confused by 'gooses', but that sometimes your code has to do the mapping, and there are plenty of edge-cases. Just avoid the issue entirely by sticking with singular throughout.If you make it so your ID is never something that Excel can incorrectly assume is a date, you're making life better for everybody.