{"id":"aeb248bf4985e8bf","repo":"encode/httpx","slug":"cannot-call-raise-for-status-as-the-request-inst","errorCode":null,"errorMessage":"Cannot call `raise_for_status` as the request instance has not been set on this response.","messagePattern":"Cannot call `raise_for_status` as the request instance has not been set on this response\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"httpx/_models.py","lineNumber":800,"sourceCode":"                # 302 (Uncacheable redirect. Method may change to GET.)\n                codes.FOUND,\n                # 303 (Client should make a GET or HEAD request.)\n                codes.SEE_OTHER,\n                # 307 (Equiv. 302, but retain method)\n                codes.TEMPORARY_REDIRECT,\n                # 308 (Equiv. 301, but retain method)\n                codes.PERMANENT_REDIRECT,\n            )\n            and \"Location\" in self.headers\n        )\n\n    def raise_for_status(self) -> Response:\n        \"\"\"\n        Raise the `HTTPStatusError` if one occurred.\n        \"\"\"\n        request = self._request\n        if request is None:\n            raise RuntimeError(\n                \"Cannot call `raise_for_status` as the request \"\n                \"instance has not been set on this response.\"\n            )\n\n        if self.is_success:\n            return self\n\n        if self.has_redirect_location:\n            message = (\n                \"{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'\\n\"\n                \"Redirect location: '{0.headers[location]}'\\n\"\n                \"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}\"\n            )\n        else:\n            message = (\n                \"{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'\\n\"\n                \"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}\"\n            )","sourceCodeStart":782,"sourceCodeEnd":818,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_models.py#L782-L818","documentation":"Raised as `RuntimeError` at the top of `Response.raise_for_status()` when `self._request is None`. `raise_for_status` needs the originating Request to attach to the `HTTPStatusError` it raises, so it bails out early if the link is missing. As with error 40, this indicates a Response that was not produced through the normal send pipeline.","triggerScenarios":"Calling `httpx.Response(404).raise_for_status()` on a hand-built Response, or calling `raise_for_status()` on a Response returned by a custom transport that did not set `request=`.","commonSituations":"Tests with synthetic error responses; custom transports / mocks that emulate server errors without wiring the request; middleware that wraps and rebuilds Responses.","solutions":["Construct the Response with a request: `httpx.Response(404, request=httpx.Request('GET', url))` before calling `raise_for_status()`.","Use `httpx.MockTransport` to fabricate responses so the request link is populated automatically.","Guard the call: `if getattr(response, '_request', None) is not None: response.raise_for_status()`.","In custom transports, always set `response.request = request` before returning."],"exampleFix":"// before\nresp = httpx.Response(404)\nresp.raise_for_status()  # RuntimeError\n\n// after\nreq = httpx.Request('GET', 'https://example.com/')\nresp = httpx.Response(404, request=req)\nresp.raise_for_status()  # HTTPStatusError, as intended","handlingStrategy":"validation","validationCode":"def can_raise(resp: httpx.Response) -> bool:\n    return getattr(resp, '_request', None) is not None","typeGuard":"import httpx\n\ndef response_has_request(resp: httpx.Response) -> bool:\n    return getattr(resp, '_request', None) is not None","tryCatchPattern":"try:\n    response.raise_for_status()\nexcept RuntimeError:\n    # no request attached - reattach or handle synthetically\n    response.request = httpx.Request('GET', 'https://example.com/')\n    response.raise_for_status()","preventionTips":["Always pass request= when constructing error Responses for tests.","Use MockTransport so the request link is automatic.","In custom transports, set response.request = request before returning."],"tags":["response","raise-for-status","request","mocking","runtime-error"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}