{"id":"a576b541fa19c3c5","repo":"psf/requests","slug":"self-status-code-server-error-reason-for-url","errorCode":null,"errorMessage":"{self.status_code} Server Error: {reason} for url: {self.url}","messagePattern":"(.+?) Server Error: (.+?) for url: (.+?)","errorType":"http","errorClass":"HTTPError","httpStatus":500,"severity":"error","filePath":"src/requests/models.py","lineNumber":1171,"sourceCode":"            try:\n                reason = self.reason.decode(\"utf-8\")\n            except UnicodeDecodeError:\n                reason = self.reason.decode(\"iso-8859-1\")\n        else:\n            reason = self.reason\n\n        if 400 <= self.status_code < 500:\n            http_error_msg = (\n                f\"{self.status_code} Client Error: {reason} for url: {self.url}\"\n            )\n\n        elif 500 <= self.status_code < 600:\n            http_error_msg = (\n                f\"{self.status_code} Server Error: {reason} for url: {self.url}\"\n            )\n\n        if http_error_msg:\n            raise HTTPError(http_error_msg, response=self)\n\n    def close(self) -> None:\n        \"\"\"Releases the connection back to the pool. Once this method has been\n        called the underlying ``raw`` object must not be accessed again.\n\n        *Note: Should not normally need to be called explicitly.*\n        \"\"\"\n        if not self._content_consumed:\n            self.raw.close()\n\n        release_conn = getattr(self.raw, \"release_conn\", None)\n        if release_conn is not None:\n            release_conn()\n","sourceCodeStart":1153,"sourceCodeEnd":1185,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/models.py#L1153-L1185","documentation":"Raised by Response.raise_for_status() in src/requests/models.py when the response status code is in the 5xx range (500 <= status_code < 600). It signals that the server (or an intermediary such as a load balancer or proxy) failed to fulfill a request that may itself have been valid; Requests surfaces it as requests.exceptions.HTTPError with the status, reason, and final URL.","triggerScenarios":"Calling response.raise_for_status() when the server returned 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, or 504 Gateway Timeout — e.g. hitting an API during a deploy, an upstream backend being down behind a reverse proxy, or a request payload that crashes the server-side handler.","commonSituations":"Transient outages and deploy windows (502/503), overloaded services shedding load (503), gateway timeouts on slow upstream calls (504), and genuine server bugs triggered by specific inputs (500). CI pipelines and cron jobs commonly hit these intermittently.","solutions":["Retry with exponential backoff for transient 5xx: mount an HTTPAdapter with urllib3.util.Retry(total=3, backoff_factor=1, status_forcelist=[500,502,503,504]).","Log response.text — many servers include a request ID or stack trace fragment useful for reporting the issue upstream.","If a specific payload reliably triggers 500, minimize and report it to the API owner; validate your input format against the API docs.","Check service status pages / health endpoints before assuming a client-side bug."],"exampleFix":"# before\nresp = requests.get(url)\nresp.raise_for_status()\n\n# after\nfrom requests.adapters import HTTPAdapter\nfrom urllib3.util import Retry\n\nsession = requests.Session()\nretry = Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504])\nsession.mount(\"https://\", HTTPAdapter(max_retries=retry))\nresp = session.get(url)\nresp.raise_for_status()","handlingStrategy":"retry","validationCode":null,"typeGuard":"def is_server_error(resp):\n    return 500 <= resp.status_code < 600","tryCatchPattern":"try:\n    resp = session.get(url, timeout=10)\n    resp.raise_for_status()\nexcept requests.exceptions.HTTPError as e:\n    if e.response is not None and e.response.status_code >= 500:\n        retry_with_backoff()","preventionTips":["Mount urllib3 Retry on the session: HTTPAdapter(max_retries=Retry(total=3, backoff_factor=1, status_forcelist=[500,502,503,504]))","Use exponential backoff with jitter; never retry in a tight loop","Respect Retry-After headers on 503","Set request timeouts so a struggling server doesn't hang your client","Only retry idempotent methods automatically (GET/HEAD/PUT/DELETE), not POST unless the API is idempotent"],"tags":["http","server-error","retry","raise-for-status","network"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}