3 ms·
There's nothing in the protocol, but in general, you should not assume non-2xx status codes have parseable payloads (more specifically, you should not assume th
by shadowgovt 4y ago
There's nothing in the protocol, but in general, you should not assume non-2xx status codes have parseable payloads (more specifically, you should not assume the format of a non-200 status code).
Reason being that any intermediary step in the chain of resolving your request can return the response. If your load balancer has fallen over? You'll get a 500 with a plaintext body. Don't parse that as JSON.
(Technically, any intermediary step might also return you a 2xx with a non-parseable body, but that scenario is far, far more rare... Mostly see it in completely misconfigured servers where instead of getting the service you're trying to talk to you get the general "Set up your site" landing page).
- zeveb 4y ago> you should not assume the format of a non-200 status code You should never assume format based on status code at all! You should detect it based on the Content-Type header. > You'll get a 500 with a plaintext body. Don't parse that as JSON. Any intermediary which returns plain text with an application/json Content-Type is badly, badly broken.
- shadowgovt 4y ago> You should detect it based on the Content-Type header In approximately a decade of working on JavaScript and TypeScript UI code, I can count on one hand the number of RPC handlers I've seen that inspect Content-Type in the success codepath. ... for that matter, I can count on one hand the number of RPC handlers I've see nthat inspect Content-Type in the failure codepath also. The common behavior is to dump body to text, log error, and recover.
- deleted 4y ago[deleted]
- dragonwriter 4y ago> There’s nothing in the protocol, but in general, you should not assume non-2xx status codes have parseable payloads (more specifically, you should not assume the format of a non-200 status code). There’s absolutely something in the protocol, and you should absolutely use the Content-Type header to determine whether there is a body and whether it is in a parseable format irrespective of the status code, except for the minority of status codes defined as not having a body (e.g., 204 & 205.)
- yencabulator 4y agoUnfortunately real-world use is `Content-Type: application/json` for everything.