News for hackers. Stuff that matters.
Re:I've been abusing HTTP Status Codes in my APIs for years by imdsm on Wednesday July 13, 2022 @10:12AM (#32083005)
I disagree that returning a 404 for `/api/v1/employees/100` is wrong. If `/api/v1/employees/100` is the resource that is being requested, yet the record doesn't exist, then the resource doesn't exist. Much like `/some_photo.jpg` not existing would return 404 if missing too.

What if we changed `/some_photo.jpg` to `/photo?id=some_photo` and it didn't exist? 404? Okay, now what if we change `/photo?id=some_photo` to `/photo?id=100`? What if then change that to `/photo/100`? At which point does it no longer become okay for the request to be tied to the resource?

`/api/v11/employees/1` may be 404, and `/api/v1/employees/100` maybe 404 too, because neither of them are found. If anything, the problem is that HTTP status codes are limited and haven't really kept up with technology. We have a few additional codes, like with Cloudflare, but for the most part, there is no community project or standard for expanding HTTP Status Codes.

Perhaps there should be.

Re:I've been abusing HTTP Status Codes in my APIs for years by m3galinux on Wednesday July 13, 2022 @01:22PM (#32085824)
Many of your examples are changing the query string, not the resource path. That's an important designation in REST semantics. REST APIs shouldn't use query strings for accessing singular records with IDs, that's what direct resource paths are for. Anything accessed with a query string should be treated as a search.

/api/v1/employees/100 - direct path to a resource. 404 if it doesn't exist.

/some_photo.jpg - direct path to a file/resource. 404 if it doesn't exist.

/photo/100 - direct path to resource: photo ID 100. 404 if it doesn't exist.

/photo?id=100 - search for a photo with ID "100". Return a code 200 with an empty results set if that ID is not found. (With optional application-specific error feedback in the response body.) 404 if /photo doesn't exist at all.

Re:I've been abusing HTTP Status Codes in my APIs for years by treis on Wednesday July 13, 2022 @11:00AM (#32083655)
The spec says:

>The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

So when you say:

>yet the record doesn't exist, then the resource doesn't exist.

So you do have a current representation for the target resource. Namely, that it doesn't exist.

Re:I've been abusing HTTP Status Codes in my APIs for years by dragonwriter on Wednesday July 13, 2022 @12:53PM (#32085369)
> So you do have a current representation for the target resource.

No.

> Namely, that it doesn’t exist.

That it isn’t a representation, it is a fact about the universe (not the resource, because existence is not a predicate) which is inconsistent with their being a representation. A resource must exist to have a representation.

Re:I've been abusing HTTP Status Codes in my APIs for years by NoGravitas on Wednesday July 13, 2022 @11:51AM (#32084383)
Yes, and sending a 404 status code is the correct and succinct way of expressing that fact.
Re:I've been abusing HTTP Status Codes in my APIs for years by treis on Wednesday July 13, 2022 @12:14PM (#32084726)
404 says you didn't find it. Not that it doesn't exist.
Re:I've been abusing HTTP Status Codes in my APIs for years by dragonwriter on Wednesday July 13, 2022 @01:37PM (#32086075)
> 404 says you didn’t find it. Not that it doesn’t exist.

Since a resource that does not exist cannot have any representation, it is therefore impossible for a representation to be found of such a resource. In fact, other than in the case where 421 is the correct response (“don’t ask me, I’m not responsible for that URL”) or where 406 is a more specific response than 404 (“I have the resource requested, but can’t produce a representation in the format you have requested, and don’t want to waste both of our time sending you a format you may not be able to use”), the only reason for a 404 is that the resource does not exist.

Re:I've been abusing HTTP Status Codes in my APIs for years by qainsights on Wednesday July 13, 2022 @10:23AM (#32083170)
Exactly, my thought as well. Particularly, from QA perspective (automation or performance testing), testers often assume `200` means everything is fine; let's move on. But in reality, it is not.
Re:I've been abusing HTTP Status Codes in my APIs for years by afiori on Wednesday July 13, 2022 @05:31PM (#32088997)
Api calls and document requests are very different. the latter are primarily interpreted by CDNs and browsers and need to work for them; the former are called by code and handled by code.
Re:I've been abusing HTTP Status Codes in my APIs for years by that_james on Wednesday July 13, 2022 @10:46AM (#32083482)
For me it boils down to this:

If you ask for an employee that doesn't exist, is it a failure? Or is it just a negative, but expected response?

Perhaps that's a stupid question though.

Re:I've been abusing HTTP Status Codes in my APIs for years by wizofaus on Wednesday July 13, 2022 @11:34AM (#32084124)
If you write an API designed to return a single entity matching the path provided, it's absolutely an error if it doesn't exist. If your API returns the simplest possible JSON representation in a success case, what JSON should it return if it doesn't exist?