HTML response with 502 error from API end-point

It’s also worth modifying your code to test the HTTP response code first, before trying to JSON parse.

5xx’s codes normally won’t return as JSON response but a Generic “everything is super broken” HTML message, because everything is super broken. So just retry the request.

And you should catch any JSON parsing errors, as while most 4xx responses will return a JSON blob describing the error, they might not always.

Same for 200’s sometimes you’ll get a 200 but something went wrong and you didn’t get JSON back, so you should code to account for the possibility of not getting JSON back.