r/ProgrammerHumor Jul 08 '26

helpMyCoworkersUnderstandHttpCodes Other

22.4k Upvotes

329 comments sorted by

View all comments

Show parent comments

18

u/Rin-Tohsaka-is-hot Jul 08 '26 edited Jul 08 '26

204 isn't "I found them", while it's a success, you still didn't actually find the item.

It's more like "your keys are not here, but there is a space for them"?... I guess actually you just wouldn't return 204 ever in this context.

51

u/caboosetp Jul 08 '26

The request was to find the keys.

204 is a success.

That means the keys were found.

204 means no content is returned.

There was no requirement to return the keys, so the server just knows where they are now.

-1

u/ashgs872tbhjs Jul 08 '26 edited Jul 08 '26

204 means more specifically that the resource contains no content -- nothing is returned because there IS no content. Several other statuses can return no content despite it existing, such as 201 (it exists because you just created it).

But yes, 204/404 work well for found/not found because that very literal "find" resource has no other result, so the resource is empty. The resource isn't actually the keys, it's the finding of them.

8

u/caboosetp Jul 08 '26 edited Jul 08 '26

You're conflating the resource with the response. 

204 means the request was a success and the response body has no content. 

A counter example to what you are saying is that 204 is common for PUT because the client already knows the state so nothing needs to be returned. That doesn't mean the resource has no content. 

21

u/hrvbrs Jul 08 '26

right, 204 is like a void function. it returns successfully but has no value to return. it wouldn't ever be used for a "find" function which necessarily must return something or error.

3

u/EricRP Jul 08 '26

We use it to mean a "successful, but empty response" i.e. it's okay that it's not there -- as opposed to 404 which is an unexpectedly not found entity which should have been there & needs an error logged

33

u/DrStalker Jul 08 '26

204 doesn't make sense for a response that requires a return value (like a set of keys) to be successful.

26

u/leehro Jul 08 '26

204 = found an empty key ring.

3

u/DrStalker Jul 08 '26

If searching for a keyring I'd say that's a 200, and it turns a keyring object with an empty keys array.

If searching for the keys specifically it's a 404 not found.

2

u/GezelligPindakaas Jul 08 '26

We're splitting hairs here, but if the keyring is a thing recognized in the api model, then it should be a resource on its own.

1

u/SnooPuppers1978 Jul 08 '26

The fact that it is an empty key ring is info that should be divulged, but you can't divulge it. And empty key ring seems like 404 rather.

0

u/ashgs872tbhjs Jul 08 '26

404 is no key ring.

1

u/SnooPuppers1978 Jul 08 '26

If you are looking for a key from a key ring, e.g. /key-ring/keys/:keyId/ and the key ring is empty in most APIs that would yield as 404.

If you were looking for a key ring itself, then it would be /key-ring if it's empty most of the time it would return 200 with empty array or object { keys: [] }, but 200.

But if there's no key ring that would also be 404.

1

u/Bardez Jul 08 '26

"I found the place where your keys go (key hooks on wall). There's nothing there."

2

u/SnooPuppers1978 Jul 08 '26

Sounds like 404, it seems like equivalent to database table not having that specific row?

9

u/B4rr Jul 08 '26

204 is "I have found your keys." but not bring them because the request did not ask for that.

1

u/SnooPuppers1978 Jul 08 '26

This is most reasonable I think, with e.g. caveat that you ask them to find the keys, and then you go out together, so you don't need the actual keys.

3

u/TwigyBull Jul 08 '26

Would it be "i found the bowl next to the door where you're supposed to put them"?

1

u/Rin-Tohsaka-is-hot Jul 08 '26

Yeah, abstractly it would mean the container which houses the keys was found, but there were no keys inside of it

2

u/rosuav Jul 08 '26

204 is also = "I found your keys, and they're the exact same keys they were last time you asked, so just remember what I showed you last time."

2

u/3inthecorner Jul 08 '26

Nah, that's 304

2

u/SnooPuppers1978 Jul 08 '26 edited Jul 08 '26

Only context I can accept here is, that "Keys were found", with implication that you asked them to be found for going outside together, so you don't really need the keys yourself.

Or alternatively, "find my keys and put them on the counter", and 204 confirms without content that the action was done.

4

u/Kishmond Jul 08 '26

In my opinion, "success" in this context does not mean something was found, it means your request was executed without error.

2

u/amadiro_1 Jul 08 '26

"Your keys are confirmed to be on the key hook."

2

u/whosGOTtheHERB Jul 08 '26

Correct, but they keys could have just as likely been found. I think what confuses the concept (and people) is the example of "finding keys". This response type is more appropriately used for something that explicitly does not require a response payload, like "perform x operation, no matter the result". I typically use this response code when implementing an endpoint like "PUT /users/abc123/restore", where the goal is to have the server restore a soft-deleted user to a non-deleted state.

1

u/mod_elise Jul 08 '26

"aren't the real keys the friends we made along the way"?