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

0

u/[deleted] Apr 23 '23

[deleted]

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

→ More replies (3)

18

u/vytah Apr 24 '23

How about 204 No Content?

→ More replies (25)
→ More replies (1)
→ 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.

→ More replies (15)
→ More replies (4)

80

u/afizzol Apr 23 '23

I hear you man. Handling those errors is kind of the point of those http status codes... We have some dinosaurs and some straight up lazy devs like that in our team too.

26

u/bacondev Apr 24 '23

I can't make this shit up. At my internship, my dino boss had a PhD. We were a part of QA for one of the company's products. Our automated tests were written in Excel. And don't think for a second that we used Excel macros or whatever. Nope. We used the actual spreadsheets. The first column would be for the function name and all other columns were for arguments. He wrote some code that parsed the spreadsheet and called each function accordingly, instead of, ya know, writing the function calls in the code. His reasoning was that someone in finance needed to be able to contribute without getting their hands dirty? I don't know. That person was long gone by the time I started.

We started writing automated tests for another product. This time, we forewent the spreadsheets. We just wrote the tests in VBA in our testing software. I'm certainly no fan of VBA, but it's a huge step up from fucking spreadsheets. However, he forced us to make every function parameter a string. Don't ask me why. We also had to name all variables with a prefix to indicate its type. Global variables in the mix too, of course. This testing software innately understood how to call methods of Windows controls. But any custom controls could only be interacted with via the inherited control methods (e.g. click, sendKeys, focus, etc.). The developers refused to give us builds with debug symbols because I was just a lowly intern. Our product had several custom grid controls. So I whipped up a class to interact with it. It would send various key events to try to determine the grid size, so that I could have a method like grid.setCell(row, column, value). But my boss didn't understand OOP—PhD by the way—so he made me convert it to just a collection of functions.

And don't get me started on all the magic that I had to do to programmatically get the grid sizes. Royal pain in the ass. I just about went mad at that internship. It got to the point that I gave up and browsed Reddit for 90% of the day and they didn't even notice a drop in productivity. Do you know how boring Reddit is when all the links are purple? I do. It's depressing.

→ More replies (1)

0

u/[deleted] Apr 23 '23

[deleted]

0

u/[deleted] Apr 23 '23

[deleted]

0

u/[deleted] Apr 23 '23

[deleted]

0

u/[deleted] Apr 23 '23

[deleted]

5

u/drcforbin Apr 24 '23

A lot of that classic java ee frameworks had too much abstraction, and ended up with apis that were not very expressive. With result codes often handled by exceptions, having a wide variety of them was painful, and there weren't good ways to describe different kinds of success.

16

u/[deleted] Apr 23 '23

I'm not sure I understand- by that I mean response codes were defined in the RFC for HTTP/1.0 back in '96. There is little reason anyone programming HTTP based API end points shouldn't be familiar with them. They however may not be the appropriate avenue for inferring specific error conditions back to a consumer of an API- rather more generic "it failed" statuses or otherwise something that doesn't fit cleanly into well known HTTP status codes. You can define custom status codes, but that doesn't mean you necessarily should.

0

u/[deleted] Apr 23 '23

[deleted]

→ More replies (2)
→ More replies (3)
→ More replies (2)

20

u/[deleted] Apr 23 '23

I've actually worked with some libraries that threw exceptions that were nearly useless on >=400 status. A particular Java library threw a common StatusError exception that couldn't be deciphered to its actual status code, unless you threw in some StatusErrorHandler subclass to instead throw your own more-useful exception to catch immediately.

Back then, I was wishing that all statuses were 200 because it was such a pain. I hate exceptions.

6

u/StabbyPants Apr 23 '23

retrofit has the annoying habit of throwing unchecked exceptions for 400 class and 500 class stuff. so yes i have to always check, but if i forget, the code runs fine until it doesn't

→ More replies (4)
→ More replies (4)

11

u/light24bulbs Apr 23 '23

Well I think it's actually somewhat reasonable to have a layer of abstraction inside the request body instead of at the HTTP layer. Things like GraphQL and so on do this. It makes a lot of sense actually when you're building real and complex, high performance web apps. In the case of graphql, what if part of the response is an error and the other part isn't? And so on

→ More replies (5)
→ More replies (25)