{"id":"9f0a568f8f9273df","repo":"encode/httpx","slug":"attempted-to-access-streaming-request-content-wit","errorCode":null,"errorMessage":"Attempted to access streaming request content, without having called `read()`.","messagePattern":"Attempted to access streaming request content, without having called `read\\(\\)`\\.","errorType":"exception","errorClass":"RequestNotRead","httpStatus":null,"severity":"warning","filePath":"httpx/_models.py","lineNumber":465,"sourceCode":"\n        auto_headers: list[tuple[bytes, bytes]] = []\n\n        has_host = \"Host\" in self.headers\n        has_content_length = (\n            \"Content-Length\" in self.headers or \"Transfer-Encoding\" in self.headers\n        )\n\n        if not has_host and self.url.host:\n            auto_headers.append((b\"Host\", self.url.netloc))\n        if not has_content_length and self.method in (\"POST\", \"PUT\", \"PATCH\"):\n            auto_headers.append((b\"Content-Length\", b\"0\"))\n\n        self.headers = Headers(auto_headers + self.headers.raw)\n\n    @property\n    def content(self) -> bytes:\n        if not hasattr(self, \"_content\"):\n            raise RequestNotRead()\n        return self._content\n\n    def read(self) -> bytes:\n        \"\"\"\n        Read and return the request content.\n        \"\"\"\n        if not hasattr(self, \"_content\"):\n            assert isinstance(self.stream, typing.Iterable)\n            self._content = b\"\".join(self.stream)\n            if not isinstance(self.stream, ByteStream):\n                # If a streaming request has been read entirely into memory, then\n                # we can replace the stream with a raw bytes implementation,\n                # to ensure that any non-replayable streams can still be used.\n                self.stream = ByteStream(self._content)\n        return self._content\n\n    async def aread(self) -> bytes:\n        \"\"\"","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_models.py#L447-L483","documentation":"This is httpx.RequestNotRead raised by the Request.content property when _content has not been materialized yet. Unlike Response.content (which triggers a read), Request.content requires you to have explicitly called request.read() (or aread()) first, because a streaming request body cannot be auto-read without side effects on the underlying generator.","triggerScenarios":"Calling request.content on a freshly built Request whose body is still a stream/generator and you have not called read(); inspecting a Request that was sent with content=<generator> before reading; mocking a Request without materializing its content.","commonSituations":"Logging/inspection middleware that reads request.content before the request is sent; test assertions on a constructed-but-unsent Request; building a Request manually with stream=<generator> and accessing .content.","solutions":["Call request.read() (sync) or await request.aread() (async) before accessing .content.","Build the Request with content=<bytes> instead of a generator so .content is immediately available.","Use request.stream only when you genuinely want streaming, and read() before inspection.","In middleware, guard: if not hasattr(request, '_content'): request.read()."],"exampleFix":"// before\nreq = httpx.Request('POST', url, content=gen)\nprint(req.content)  # RequestNotRead\n// after\nreq = httpx.Request('POST', url, content=gen)\nreq.read()\nprint(req.content)\n# or simpler:\nreq = httpx.Request('POST', url, content=b'...bytes...')\nprint(req.content)","handlingStrategy":"validation","validationCode":"# Ensure the request body is materialized before accessing .content\nif not hasattr(request, '_content'):\n    request.read()  # sync; use await request.aread() for async\nbody = request.content","typeGuard":"def request_content_ready(request: 'httpx.Request') -> bool:\n    return hasattr(request, '_content')","tryCatchPattern":"try:\n    body = request.content\nexcept httpx.RequestNotRead:\n    request.read()\n    body = request.content","preventionTips":["Call request.read()/aread() before inspecting .content.","Build Requests with content=<bytes> when you need immediate .content access.","In middleware, guard: if not hasattr(request, '_content'): request.read().","Avoid passing generators as content for Requests you intend to inspect."],"tags":["streaming","request-content","api-misuse"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}