{"record":{"id":"0b0ded91b3e2c5bc","repo":"locustio/locust","slug":"cannot-iterate-content-on-a-response-without-resp","errorCode":null,"errorMessage":"Cannot iterate content on a response without _response attribute","messagePattern":"Cannot iterate content on a response without _response attribute","errorType":"exception","errorClass":"LocustError","httpStatus":null,"severity":"error","filePath":"locust/contrib/fasthttp.py","lineNumber":590,"sourceCode":"    def raise_for_status(self):\n        \"\"\"Raise any connection errors that occurred during the request\"\"\"\n        if error := getattr(self, \"error\", None):\n            raise error\n\n    def iter_content(self, chunk_size=1024, decode_content=True):\n        \"\"\"\n        Simulates the `requests.Response.iter_content` method\n\n        Used for streaming response content\n\n        :param `chunk_size`: The size of the chunk read each time\n\n        :param `decode_content`: Whether to decode the content (from bytes to string)\n\n        :return: A generator that produces one chunk at a time\n        \"\"\"\n        if not self._response:\n            raise LocustError(\"Cannot iterate content on a response without _response attribute\")\n\n        while True:\n            try:\n                chunk = self._response.read(chunk_size)\n                if not chunk:\n                    break\n\n                if decode_content and isinstance(chunk, bytes):\n                    try:\n                        chunk = chunk.decode(\"utf-8\")\n                    except UnicodeDecodeError:\n                        # If decoding fails, preserve the data in byte format.\n                        pass\n\n                yield chunk\n            except (HTTPConnectionClosed, ConnectionError):\n                break\n","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/contrib/fasthttp.py#L572-L608","documentation":"FastResponse.iter_content streams chunks from the underlying geventhttpclient response stored in self._response. If the wrapper has no _response attribute (response not backed by a real HTTP response), it raises LocustError rather than failing silently.","triggerScenarios":"Calling `iter_content()` (directly or via `iter_lines()`) on a FastResponse that lacks `_response`, e.g. a manually constructed FastResponse or one from a non-standard code path.","commonSituations":"Wrapping/instantiating FastResponse in custom code or tests without an underlying response; mocking responses and then calling streaming methods; using iter_lines on error-path responses.","solutions":["Only call iter_content/iter_lines on responses returned from actual FastHttpSession requests","If constructing FastResponse manually, set `_response` to a valid response-like object with `.read(chunk_size)`","Check the response is from a successful request before streaming"],"exampleFix":"// before\nresp = FastResponse()  # no _response\nfor line in resp.iter_lines(): ...\n// after\nwith self.client.get(\"/stream\", catch_response=True) as resp:\n    for line in resp.iter_lines():\n        ...","handlingStrategy":"type-guard","validationCode":"def can_stream(resp):\n    return getattr(resp, \"_response\", None) is not None","typeGuard":"def is_streamable(resp) -> bool:\n    return getattr(resp, \"_response\", None) is not None and hasattr(resp._response, \"read\")","tryCatchPattern":"try:\n    for chunk in resp.iter_content(1024):\n        ...\nexcept LocustError as e:\n    logger.error(\"response not streamable: %s\", e)","preventionTips":["Only stream responses from real FastHttpSession requests","Avoid constructing FastResponse manually outside tests","Guard streaming helpers with a _response check"],"tags":["locust","fasthttp","streaming","response"],"backgroundTag":"response-not-initialized","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}