Repository navigation
Add Response.raw for reading the body into a buffer - #219
Merged
Merged
Conversation
Response.raw is a file-like object with read() and readinto(), as in CPython's requests. With stream=True, raw.readinto() reads a large body into a buffer that is reused, so it needs no further memory. json() already used this object internally. raw.read(size) now reads through the response like readinto() does. It read the socket directly, so it lost the start of the body that was read along with the headers, and ignored Content-Length and chunked encoding. The socket docstring now points to raw for the same reason. On a PicoPad (RP2040), 10 downloads of a 43 KB body into a 1 KB buffer left free memory and the largest free block unchanged. Reading the same body with .content fails with MemoryError on the first download.
adafruit-adabot
pushed a commit
to adafruit/Adafruit_CircuitPython_Bundle
that referenced
this pull request
Oct 31, 2026
Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 4.1.18 from 4.1.17: > Merge pull request adafruit/Adafruit_CircuitPython_Requests#218 from bablokb/fix-host-header > Merge pull request adafruit/Adafruit_CircuitPython_Requests#219 from MakerClassCZ/response-raw Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA: > Added the following libraries: Adafruit_CircuitPython_JamPacked
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Response.rawis a file-like object withread()andreadinto(), as in CPython's requests. Withstream=True,raw.readinto()reads a large body into a reused buffer.json()already used this object internally.raw.read(size)now reads the same way asreadinto(). It read the socket directly, so it missed the body bytes already buffered with the headers (3 bytes in my test) and ignoredContent-Lengthand chunked encoding. For the same reason, theResponse.socketdocs now say to read the body withResponse.raw.On a PicoPad (RP2040), 10 downloads of a 43 KB body into a 1 KB buffer left free memory and the largest free block unchanged. With .content, the first download fails with MemoryError.