Skip to content

Add Response.raw for reading the body into a buffer - #219

Merged
tannewt merged 1 commit into
adafruit:mainfrom
MakerClassCZ:response-raw
Oct 19, 2026
Merged

tannewt merged 1 commit into
adafruit:mainfrom
MakerClassCZ:response-raw

Conversation

@lynt-smitka

Copy link
Copy Markdown
Contributor

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 reused buffer. json() already used this object internally.

raw.read(size) now reads the same way as readinto(). It read the socket directly, so it missed the body bytes already buffered with the headers (3 bytes in my test) and ignored Content-Length and chunked encoding. For the same reason, the Response.socket docs now say to read the body with Response.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.

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.

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks!

@tannewt
tannewt merged commit 8d8b110 into adafruit:main Oct 19, 2026
1 check passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants