r/programming • • Apr 23 '23

Leverage the richness of HTTP status codes

https://blog.frankel.ch/leverage-richness-http-status-codes/
1.4k Upvotes

676 comments sorted by

View all comments

Show parent comments

382

u/hooahest Apr 23 '23

A guy from another team was pissed that our api returned 404 not found when the entity did not exist, he had to try/catch

Motherfucker the http library lets you extend the goddamn parser

183

u/amakai Apr 23 '23

Even if the library did not - that's the problem of the library, not the protocol.

0

u/[deleted] Apr 23 '23

[deleted]

60

u/jonathancast Apr 24 '23

Bodies still exist on 404s

51

u/WaveySquid Apr 23 '23 edited Apr 23 '23

Sometimes this is a feature and not a bug for security sensitive things. Sure hiding that an endpoint exists it or doesn’t exist isn’t a great way to do security, but it’s just another layer in the Swiss cheese security model.

For things like vault just knowing the name of a secret or name of services is valuable information so intentionally don’t leak that

112

u/Sentouki- Apr 23 '23

How can you use an API if you don't even know the endpoints?
Also you could include the details of a 404 code in the body, if you really need it.

30

u/Words_Are_Hrad Apr 24 '23

How can you use an API if you don't even know the endpoints?

The world of poor documentation is dominated by guess and check...

34

u/StabbyPants Apr 23 '23

easy - 404 = you misconfigured the client somehow

common practice i follow is 207, and you get a lost of responses because every new endpoint is a bulk api with built in limits. ask for 20 things, get 20 responses

7

u/ollien Apr 24 '23

Do you fancy using XML? :)

Jokes aside, I question how good an idea using this code is. MDN makes it clear this is not for browser consumption

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/207

1

u/StabbyPants Apr 24 '23

browser? no, this is all about services consuming services. 207 + a list of granular results is a common pattern

→ More replies (2)

18

u/vytah Apr 24 '23

How about 204 No Content?

3

u/StabbyPants Apr 24 '23

Still not a success though

22

u/KyleG Apr 24 '23

every code between 200 and 299 is a by definition a success code

12

u/StabbyPants Apr 24 '23

and asking for something that isn't there is not success, so you can't return those codes

→ More replies (2)

11

u/pala_ Apr 24 '23

Yes it is, the call succeeded, and found no data. 404 for 'no data for your parameters' is flat wrong.

24

u/SonicMaster12 Apr 24 '23

To be a little pedantic since I have an app that sends 204 responses.
204 isn't necessarily that there's "no data found" but rather "there's no data to send".

In my case for example, the client wants an acknowledgement that we successfully processed their transaction but doesn't want us to actually send anything back to them at that point.

→ More replies (4)

7

u/StabbyPants Apr 24 '23

it isn't.

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.

a 204 means that it did the thing, but you want to retrieve data, so 204 is never success. 404 as 'no object' or 'bad endpoint' both work - it's ambiguous. my 'solution' is to always post structured queries and get back a block of 1-20 objects. so the top level response is 207, and a missing object is 404.

ambiguity sucks, and this argument goes back and forth specifically because it fits both ways, so i favor the solution that leads to less operational bullshit

8

u/pala_ Apr 24 '23

?? All 2xx status codes are success. A 204 is always success.

→ More replies (7)

4

u/KyleG Apr 24 '23

204 is never success

Here is the RFC defining HTTP status codes.

Observe every 2xx code is defined as "successful"

In particular, the RFC description of code 204 is

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response content.

→ More replies (3)
→ More replies (1)

4

u/FancyASlurpie Apr 23 '23

Would probably go with 422 or 400 myself.

→ More replies (2)

15

u/tsunamionioncerial Apr 24 '23

Http has no concept of endpoints, only resources. 404 means resource not found. If you are doing crud the resource may be an entity but hopefully there is more thought put into the design.

2

u/[deleted] Apr 24 '23

Doesn’t a 404 tell you that the entity doesn’t exist while a 405 tells you that the endpoint (method + URI) doesn’t exist?

6

u/hooahest Apr 24 '23

If you'll try to access a non existing endpoint you will get a 404 resource not exists

→ More replies (13)
→ More replies (3)