News for hackers. Stuff that matters.
Re:Some items from my “reliability list” by pjungwir on Thursday July 25, 2019 @11:39AM (#20526304)
> Also, if you are literally having HTTP 400s internally, why aren't you using some kind of actual RPC mechanism? Do you like pain?

I just had a discussion about this yesterday where we have an internal JSON API that auths a credit card, and if the card is declined it returns a status and a message. Another developer wanted it to return a 4xx error, but that made me uneasy. I think you could make a good argument either way, but to me that isn't a failure you'd present at the HTTP layer. 4xx is better than 5xx, but I was still worried how intermediate devices would interfere. (E.g. an AWS ELB will take your node out of service if it gives too many 5xxs, and IIS can do some crazy things if your app returns a 401.) Also I don't want declined cards to show up in system-level monitoring. But what do other folks think? I believe smart people can make a case either way.

EDIT: Btw based on these Stack Overflow answers I'm in the minority: https://stackoverflow.com/questions/9381520/what-is-the-appr...

Re:Some items from my “reliability list” by citrusx on Thursday July 25, 2019 @12:51PM (#20527009)
In my opinion, a rejection is an expected outcome, and therefore should have a response code of 200. You're not asking, "Does this card exist?", and sending a 404 if you have no record. You're asking a remote system to do a job for you, and if that job completes successfully, it's a "Success" in the HTTP world.

At that point, you rely on the body content to tell you what the service correctly determined for you. A result that the user doesn't like is way different than a result that comes about because something was done wrong at the client side (4xx) or a failure on the server side (5xx).

Re:Some items from my “reliability list” by bandushrew on Thursday July 25, 2019 @11:00PM (#20532001)
Its the difference between whether you believe you are using HTTP as a transport mechanism or you believe you are using it to create a DSL protocol.

Personally I prefer to think of HTTP as a transport mechanism, with business logic at a higher level. The question "If I replaced this http transport with, say, FTP or a dvd drop, would it require changes at the business logic level?" feels important to me.

That preference puts me on the wrong side of history at the moment though, and Im going with history. Lets express our business logic through the error system provided by our transport mechanism instead....

Re:Some items from my “reliability list” by sagichmal on Thursday July 25, 2019 @02:17PM (#20527901)
> You're asking a remote system to do a job for you, and if that job completes successfully, it's a "Success" in the HTTP world.

But a provided credit card number not existing is not success, it is unambiguously a failure.

HTTP is a transport but its response codes are clearly designed to map somehow to the "business logic" of the thing underneath it. We have to twist ourselves up in knots to map most of our business systems to the set of status codes that make sense for the default HTTP-fronted application (Fields' thesis stuff) but that's what we sign up for when we decide to expose our business services via HTTP. Other transports require different contortions.

Re:Some items from my “reliability list” by treis on Thursday July 25, 2019 @03:05PM (#20528382)
>But a provided credit card number not existing is not success, it is unambiguously a failure.

But the failure is ambiguous. If I get a 404 back I can't rely on that to mean the credit card doesn't exist. It could mean that I have the wrong URL or that the service application screwed up their routing code.

Re:Some items from my “reliability list” by nulagrithom on Thursday July 25, 2019 @03:11PM (#20528448)
You wouldn't put a card number in the path anyway (for obvious reasons). Far more sensible to put that in the request body.

And who's to say you can't put the reason in the body and still keep the code? What are you hurting by sending back 400? Unless you have lb's taking out nodes because of excessive 4xx's (which sounds like insanity) I don't see a reason _not_ to send 4xx's. At the very least it's a useful heuristic tool.

Re:Some items from my “reliability list” by tlynchpin on Thursday July 25, 2019 @09:12PM (#20531454)
What are the obvious reasons? I'll presume you are referring to disclosure of the card number.

I had this discussion recently about 'security' with regard to X-Header versus ?query=param. Either it's http all plaintext on the network or it's http with tls all cyphertext on the network. Every bit in the http request and response is equivalent - verb, path, headers, body, etc - agree?

You could represent the card number as cyphertext in the request body, that's a good practice regardless of tls, but of course don't roll your own crypto. You could put that cyphertext in the path as well but if the cyphertext isn't stable that makes for a huge mess of paths.

You could make a case for trad 'combined' access logs situation with the path disclosed in log files. I can appreciate keeping uris 'clean' makes it safe to integrate a world of http monitoring tools, I would make this argument. In the case of the card represented in a stable cyphertext it's kinda cool to expose it safely to those tools.

Anything else?

Re:Some items from my “reliability list” by tda on Thursday July 25, 2019 @11:57PM (#20532244)
If you grab something from an external service (say a cdn) then I believe by default the referrer will contain the url + query params, but not you X=supersecret header. Bit me once
Re:Some items from my “reliability list” by treis on Thursday July 25, 2019 @03:54PM (#20528918)
>And who's to say you can't put the reason in the body and still keep the code?

Often systems will have application wide error handling to catch that and handle it in a systemic way. It can be a pain to short circuit that in a customizable off the shelf applications like Salesforce.

Philosophically, 4XX means the client did something wrong. Sending invalid data to a validation service is not doing something wrong.

Re:Some items from my “reliability list” by sagichmal on Thursday July 25, 2019 @04:43PM (#20529484)
> Philosophically, 4XX means the client did something wrong. Sending invalid data to a validation service is not doing something wrong.

The precise definition of 400 Bad Request is

    The 400 (Bad Request) status code indicates that the server cannot or
    will not process the request due to something that is perceived to be
    a client error (e.g., malformed request syntax, invalid request
    message framing, or deceptive request routing).
There's some room for interpretation for the phrase "process the request". If the only job of the service is to validate the request and return Valid or Invalid, then I guess yeah even an invalid request will have been processed, and 400 Bad Request may not be totally right. I think you could make a case for it! But I think you could also make a case for 200 OK with a more detailed error message and code in the body.

But if the service both validates the request and _then_ does something more substantial with it, I think 400 Bad Request is probably the most appropriate response to something that fails at step one.

Re:Some items from my “reliability list” by stingraycharles on Thursday July 25, 2019 @02:49PM (#20528233)
Exactly. The parent is thinking along the lines of “but when the error is thrown successfully, it actually means success!” and at the same time missing out on an opportunity to leverage a well-understood error handling standard in HTTP.
Re:Some items from my “reliability list” by nulagrithom on Thursday July 25, 2019 @03:07PM (#20528397)
Agreed. What else would you use 400 for? I don't see how sending back 400 is going to hurt.

The payoff of using 400 is you can watch your 400 rates with almost no effort (HTTP is well established and there's many many tools out there.) If you somehow start accidentally munging the card number sometimes or if your card processor starts doing wacky stuff you'll see a spike in 400 rates.

If it was really that troubling that declined cards are expected, I would personally at least want to see 200 come from the internal API and 400 go out to the client.

And if your "intermediate devices" start doing goofy stuff to 400's then you've got bigger problems... 4xx's shouldn't be taking nodes out of prod. That's wack.

Re:Some items from my “reliability list” by neop1x on Friday July 26, 2019 @12:05PM (#20535810)
In my opinion 404 should really mean the API endpoint doesn't exist and therefore the requested action did nothing. Using HTTP for business logic is like using ethernet frames with parts of IP. It is another level, HTTP is just a transport. 404 - not found, 5xx server problem, 301 - endpoint is now elsewhere etc. HTTP is for browser pages but you are actually making API requests using your JSON(or urlencoding or whatever)-based application protocol.
Re:Some items from my “reliability list” by stingraycharles on Thursday July 25, 2019 @04:23PM (#20529243)
> 4xx's shouldn't be taking nodes out of prod. That's wack.

Welcome to AWS, where this is actually part of standard procedure (at least Elastic Beanstalk does this, not sure if it’s actually Elastic Loadbalancer under the hood).

Re:Some items from my “reliability list” by therein on Thursday July 25, 2019 @10:52PM (#20531975)
Don't you love it when all configured backends/origins for a service are marked unhealthy through healthchecks, Amazon's load balancers route traffic to all origins.
Re:Some items from my “reliability list” by Aeolun on Friday July 26, 2019 @05:34AM (#20533512)
I mean, the alternative is routing it nowhere. Might as well throw it at the wall and see if it sticks.
Re:Some items from my “reliability list” by kyle-rb on Thursday July 25, 2019 @03:52PM (#20528894)
>What else would you use 400 for?

400 is "bad request"; you might use it if the request body was not valid JSON.

Re:Some items from my “reliability list” by hugelgupf on Thursday July 25, 2019 @02:45PM (#20528198)
It is a failure, but not of the HTTP service. It's a failure of the RPC call.

404 means "this API doesn't exist", not "the API exists but it returns an error"

Re:Some items from my “reliability list” by sagichmal on Thursday July 25, 2019 @02:51PM (#20528252)
404 does not mean "this API doesn't exist". It means, quote,

    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.
Resource here is a transport-level (HTTP) representation of a business-logic concept, in most cases a document, but if your HTTP server happens to front a credit card validation service, potentially a credit card. Or a user. Or a recipe.
Re:Some items from my “reliability list” by p1necone on Thursday July 25, 2019 @04:12PM (#20529096)
I typed up a big spiel about the difference between "expected in business logic failures" and "infrastructure failures", but then I looked at the wikipedia page for HTTP error codes and it does look like some HTTP 400 level errors are intended for cases like clients sending data that fails business logic level validation and the like.
Re:Some items from my “reliability list” by sagichmal on Thursday July 25, 2019 @04:16PM (#20529140)
That's right.

And let me say it's totally valid to just ignore that advice, and say that here, in this company, we use 200 OK for everything that isn't a parse failure. And that's not totally wrong! It won't break anything. You're not really leveraging any of the power of your transport but maybe that's fine for your situation.

Re:Some items from my “reliability list” by meowface on Thursday July 25, 2019 @01:56PM (#20527698)
I think this is the exact right way of looking at it. 4xx/5xx are for layers that are below the business logic (like "is the web server able to route this path", not "did the application successfully validate the user's input data and allow them to continue on to the next step in a process").

It can get a little fuzzy for 401 and 403 errors, though; those seem to be returned by APIs pretty often. Not entirely sure how I feel about that, but I think those are a bit more sensible.

Re:Some items from my “reliability list” by jacobgreenleaf on Thursday July 25, 2019 @02:09PM (#20527833)
The HTTP IETF RFC (2616) states:

> The first digit of the Status-Code defines the class of response. The last two digits do not have any categorization role. There are 5 values for the first digit:

> - 4xx: Client Error - The request contains bad syntax or cannot be fulfilled

What do you think status codes like 404, 405, 406 are for? You say they shouldn't be for "did the application successfully validate the user's input data" but status code 400 is explicitly for bad requests. In your view should a HTTP server ever return 4xx?

Re:Some items from my “reliability list” by meowface on Thursday July 25, 2019 @03:07PM (#20528401)
It depends how you define validate. If the HTTP headers are malformed, sure. If a JSON or XML API receives malformed JSON or XML, sure. But if it's "register this account" and the username already exists, or "pay with this credit card" but it's not a valid credit card number, or "process this invoice" but the total is missing, then I think it should be a 200 status with an error message. It's validating the request was received and loaded by the application vs. validating the business logic requirements for the request's data payload.

Obviously if you send a TAIL method request, you should get a 405, and if you send Accept: eggs/*, you should get a 406. If a route doesn't exist, you should get a 404. If you fail HTTP basic auth, you should get a 403 (but why are you using HTTP basic auth?). If you want certain paths to never be accessed for some reason, you should return a 401.

Re:Some items from my “reliability list” by stcredzero on Thursday July 25, 2019 @02:34PM (#20528088)
You're not asking, "Does this card exist?", and sending a 404 if you have no record.

Doesn't this depend on the nature of the REST API? (Though putting a credit card number on the path doesn't seem like a good practice.)

Re:Some items from my “reliability list” by parliament32 on Thursday July 25, 2019 @12:48PM (#20526961)
I think you're right. 4XX means "client made an error" at the application layer. Something like a malformed request (say, bad JSON passed in): absolutely, 4XX is the way to go. However, if the client sent a good message who's contents happen to be rejected, that's a 200 (and whatever error goes in the response body). The whole "did this card work" question happens in a layer above the application layer, and HTTP status codes are only meant for the application layer itself.
Re:Some items from my “reliability list” by sagichmal on Thursday July 25, 2019 @02:21PM (#20527947)
> HTTP status codes are only meant for the application layer itself.

You seem to be using a strange definition of "application", but assuming you mean "transport" — I mean, this is obviously not true, right?

If you sent a perfectly semantically valid HTTP request for a resource that doesn't exist on the remote, that's a 404. But by your logic, you're saying it should be a 200, because there wasn't a failure in (essentially) nginx or the HTTP library of your application server?

HTTP is a transport designed for a specific type of application (document-oriented resources) and its status codes (and lots of other things) reflect that initial coupling, so it's always gonna be a pain when you swap out the application (to a credit card processing service or whatever) and have to think about how the status code mapping will work. And you can certainly "cheat" and say that every request that doesn't crash the server is 200 OK, I guess, but that's pretty clearly not what the Hyper Text Transfer Protocol wants you to do. Its set of status codes clearly reflect details of the underlying application.

Re:Some items from my “reliability list” by parliament32 on Thursday July 25, 2019 @05:46PM (#20530099)
Transport is TCP/UDP/etc. HTTP is part of the application layer, see https://en.wikipedia.org/wiki/OSI_model and https://en.wikipedia.org/wiki/Application_layer.

>If you sent a perfectly semantically valid HTTP request for a resource that doesn't exist on the remote

Requesting a path (via the application layer) that doesn't exist is a client error, hence the 4XX. But, say, if your website has a search bar and a client searches for "blahblah-thisdoesntexist" and gets no results, the webpage should return a 200 (the request was valid: the client sent form data to an existing resource and it was processed successfully) with something like "no results found" in the body -- because the search function itself is above the application layer. This is what I mean about "is the CC is authorized or not" being above the application layer.

>Its set of status codes clearly reflect details of the underlying application.

This is where I think you're wrong: HTTP status codes reflect the result of the HTTP request itself, not the next application in the chain, if one exists.

This can also be expressed if we look lower down the chain. If my HTTP request gave me a 404, should an error be shown in the TCP stack? Well no, because TCP doesn't know or care about HTTP, as far as it's concerned the TCP connection is established and happy. Likewise, in this example, the API HTTP stack doesn't care about whether the data was a CC or something else or what the result was: as far as the HTTP stack is concerned, some JSON got passed in and parsed properly, and some output got sent back, and everything went well so "200".

Re:Some items from my “reliability list” by sagichmal on Thursday July 25, 2019 @07:18PM (#20530819)
> OSI

I understand OSI, but it's not a useful model in this circumstance, because it draws no distinction between the HTTP layer and the business logic served over HTTP. The complexities of that interface is what we're talking about.

> HTTP status codes reflect the result of the HTTP request itself, not the next application in the chain, if one exists.

Let's say I request /robotz.txt and I get 404 Not Found.

How do you maintain that 404 is a result of "the HTTP request itself" and not the HTTP server handing the request to the component that looks on the filesystem (or in the cache, or in MongoDB, or...) and not finding the resource?

Re:Some items from my “reliability list” by msluyter on Thursday July 25, 2019 @11:49AM (#20526399)
Interesting question. My understanding is that a 400 response indicates that there's something malformed about the request itself such that the server can't/won't process it. Given that in order to decline the card the service has to actually process the request, I'd agree that a 400 is inappropriate.
Re:Some items from my “reliability list” by Shaanie on Thursday July 25, 2019 @02:03PM (#20527771)
The previous definition was 400 means the server can't understand the request. Current one is

>The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Of course, a lot of things could be said to be a client error.

Re:Some items from my “reliability list” by munchbunny on Thursday July 25, 2019 @02:53PM (#20528273)
I would agree with not using the http status codes to return a card processing error.

Specifically, you want to be able to easily distinguish between "your url is wrong" or "your authentication credentials are wrong" or "the API endpoint threw an exception" type errors and "credit card processing failed" errors.

It's easier long run to put business logic errors somewhere separate from protocol/routing layer (i.e. even a http header would be better) so that you can tell what is Rails/Flask/whatever failing vs. logic failing. This also gives you more flexibility to do stuff at the hardware layer (another commenter mentioned ELB) without interfering with the application layer.

Re:Some items from my “reliability list” by erpellan on Thursday July 25, 2019 @03:29PM (#20528629)
4xx errors are no better or worse than 5xx. They just mean different things.

Generally, 4xx errors means 'Client screwed up'. There's probably no point sending this message again, it isn't going to work until something is fixed. That might mean it will work if, for example, the account is funded or card unlocked. But something needs to be done on behalf of the client.

5xx errors mean 'Server screwed up'. It probably _is_ worth having another attempt at sending the request. Maybe it was a temporary glitch, or maybe a new release of the server has to happen. Regardless, there was probably nothing inherently wrong with the actual request.

Re:Some items from my “reliability list” by xorcist on Thursday July 25, 2019 @05:09PM (#20529735)
Using HTTP for RPC lends itself to this sort of endless discussion. Authorization is even worse.

You could argue either way. What's important is being consistent, at the very least throughout an API but preferably throughout the whole organization.

(Personally I'd probably lean towards a 40x of some kind, just make sure it doesn't clash with something that you care about.)

Along the same lines, and arguably more important, is how to log the operations where a transaction completed successfully but with a negative answer. If you log expected negatives as errors you can get error blindness.

Re:Some items from my “reliability list” by russelldavis on Thursday July 25, 2019 @01:04PM (#20527196)
Stripe returns HTTP 402 ("Payment Required") when a card is declined.
Re:Some items from my “reliability list” by paulddraper on Thursday July 25, 2019 @01:19PM (#20527368)
I think the key is

> A "HTTP 400 bad request" is only the sort of thing you get to ignore when it's coming from the great unwashed masses who are lobbing random crazy crap at you, trying to break you. Internally, that's something entirely different.

If this is an internal-only API then most people look at 400s as their bug. Sounds like you are in the same boat.

If it's an external API, it's not their bug.

Re:Some items from my “reliability list” by erikpukinskis on Thursday July 25, 2019 @11:52AM (#20526418)
I always thought, if there’s a code that officially signals what we’re trying to signal, we should use it, and rely on it as “the signal”.

But at the end of the day, your app must target a subset of available APIs. If you use every available API the surface will be too big and you will have snowballing complexity. So if you are already using another API which can signal what you’re trying to signal then feel free to institute a “everything we can expect returns 200” policy. That kind of decision is critical for putting an upper bound on your system complexity.

I want to institute a “no default exports” policy in the JavaScript app I’m working on. It’s not that there aren’t places a default export makes sense—there are. It’s that prohibiting them will decrease the number of things our coders have to think about.

Re:Some items from my “reliability list” by s_kilk on Thursday July 25, 2019 @01:26PM (#20527420)
> I always thought, if there’s a code that officially signals what we’re trying to signal, we should use it, and rely on it as “the signal”.

This is a line I hear a lot in regards to this general situation, and the interesting part is that the 400 code really does not signal the thing the developer is trying to signal, not at all. But a lot of devs have a weird hang-up about this for some reason, and will go to any lengths to not send a 200 response. I blame the REST cultists.

Re:Some items from my “reliability list” by kbenson on Thursday July 25, 2019 @01:45PM (#20527601)
Well, 4XX are for client errors, so if the client fails authing to the API, it would be appropriate there. If the API fails authenticating to an API further down the chain, that shouldn't be returned as a 4XX status code, because it could be confused with the immediate API's auth.

HTTP status codes can be used to good effect if done thoughtfully, because there's often already a lot of logic in the useragent to handle some cases (such as redirects, etc). Not using anything except for 200 is limiting. Using a bunch of status codes to refer to things that make no sense is confusing. Defining a sane standard for how your application will choose between them that allows for the benefits they offer without confusing clients as to what is actually being communicated is the best of both though.

Re:Some items from my “reliability list” by hathawsh on Thursday July 25, 2019 @04:04PM (#20529015)
For the HTTP API my company has been building, we're returning 400 status codes for most errors. It is intended as a strong suggestion that client code needs to handle errors rather than ignore them. However, we're aware that some client libraries are limited in their ability to handle status codes other than 2xx, so we allow an optional request parameter that asks the server to return 200 instead of 400 for most errors. As you said, smart people disagree about this, so we let them choose. I think we've hit a good balance.
Re:Some items from my “reliability list” by DreamSpinner on Friday July 26, 2019 @01:07AM (#20532492)
That sounds like an excellent approach. If a consumer chooses "always 200 on error", how do you return the API response status? HTTP Response Header or in a custom value in the response body?
Re:Some items from my “reliability list” by hathawsh on Friday July 26, 2019 @01:09PM (#20536338)
Thanks. Error responses from our API always generate a JSON object with top level error and error_description attributes, while non-errors never contain a top level error attribute. It might be interesting to add a response header to disambiguate even more.
Re:Some items from my “reliability list” by neop1x on Friday July 26, 2019 @12:15PM (#20535889)
I came to a good analogy - Imagine sending that card number using HTTP form to a HTML target URL. You wouldn't return 4xx code if a card is not found. The user would see your 404 page or browser 404 page and think the page doesn't exist! API shouldn't be different.
Re:Some items from my “reliability list” by z3t4 on Thursday July 25, 2019 @04:00PM (#20528975)
I think the pain comes from not controlling the full stack. But managing the full stack is also painful. Having several abstraction layers will affect performance. Personally I agree that's its not a good idea to reuse (HTTP) error codes in your app layer ala REST
Re:Some items from my “reliability list” by geezerjay on Thursday July 25, 2019 @09:47PM (#20531662)
IMHO returning status 400 is only reasonable if the request itself is malformed. A request to check whether a CC is valid does not become malformed depending on whether the CC is valid or not.
Re:Some items from my “reliability list” by brown9-2 on Thursday July 25, 2019 @05:53PM (#20530167)
It sounds like a lot of the problems you describe come from using software that assumes normal “HTTP web server” applications when you really have a custom web application.