It's a file-like object. Read them:

>>> b = io.BytesIO(b'hello')
>>> b.read()
b'hello'

If the data coming in from body is too large to read into memory, you'll want to refactor your code and use zlib.decompressobj instead of zlib.decompress.

Answer from wim on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-from-_io-bytesio-to-a-bytes-like-object-in-python
Convert from '_Io.Bytesio' to a Bytes-Like Object in Python - GeeksforGeeks
July 23, 2025 - To convert from _io.BytesIO to a bytes-like object using the getvalue() method, we can directly obtain the byte data stored in the BytesIO buffer.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-io-bytesio-stringio
Python io.BytesIO and io.StringIO: Memory File Guide | DigitalOcean
August 3, 2022 - Just like what we do with variables, data can be kept as bytes in an in-memory buffer when we use the io module’s Byte IO operations. Here is a sample program to demonstrate this: import io stream_str = io.BytesIO(b"JournalDev Python: \x00\x01") print(stream_str.getvalue()) Let’s see the output for this program: The getvalue() function just takes the value from the Buffer as a String.
🌐
Python
docs.python.org › 3 › library › io.html
io — Core tools for working with streams
Buffered I/O streams provide a higher-level interface to an I/O device than raw I/O does. ... A binary stream using an in-memory bytes buffer. It inherits from BufferedIOBase. The buffer is discarded when the close() method is called. The optional argument initial_bytes is a bytes-like object that contains initial data. BytesIO provides or overrides these methods in addition to those from BufferedIOBase and IOBase:
🌐
Python⇒Speed
pythonspeed.com › articles › bytesio-reduce-memory-usage
The surprising way to save memory with BytesIO
February 27, 2025 - How does this work? BytesIO is using copy-on-write. Internally, it keeps a reference to the new bytes object returned from getvalue(). So long as you don’t write to the BytesIO, any reads can happen off the same memory.
🌐
Pynerds
pynerds.com › io-bytesio-in-python
io.BytesIO in Python
Disclaimer: References to any specific company, product or services on this Site are not controlled by GoDaddy.com LLC and do not constitute or imply its association with or endorsement of third party advertisers.
🌐
Finxter
blog.finxter.com › home › learn python blog › converting python bytes to bytesio objects
Converting Python Bytes to BytesIO Objects - Be on the Right Side of Change
February 23, 2024 - This example demonstrates writing multiple bytes chunks to a BytesIO object sequentially. This is useful for processing data that comes in parts or streaming large amounts of data. For quick one-off tasks, Python’s generator expressions can be used to combine several byte strings and convert them to a BytesIO object in a single line of code.
🌐
DNMTechs
dnmtechs.com › converting-_io-bytesio-to-a-bytes-like-object-in-python-3-6
Converting ‘_io.BytesIO’ to a bytes-like object in Python 3.6 – DNMTechs – Sharing and Storing Technology Knowledge
In this example, we convert a file-like object, created using the ‘io.BytesIO’ class, to a bytes-like object using the ‘read’ method. The ‘read’ method reads the contents of the file-like object and returns a bytes-like object. The resulting bytes-like object is then printed. bytearray_object = bytearray(b"Hello, World!") bytes_object = bytes(bytearray_object) print(bytes_object)
Find elsewhere
🌐
Python
wiki.python.org › moin › BytesIO
BytesIO - Python Wiki
April 28, 2011 - 6 7 >>> b = bytes() 8 >>> f = BytesIO(b, 'w') 9 >>> f.write(bytes.fromhex('ca fe ba be')) 10 >>> f.write(bytes.fromhex('57 41 56 45')) 11 >>> b 12 bytes([202, 254, 186, 190, 87, 65, 86, 69]) 13 """ 14 15 def __init__(self, buf, mode='r'): 16 """ Create a new BytesIO for reading or writing the given buffer.
🌐
GeeksforGeeks
geeksforgeeks.org › python › stringio-and-bytesio-for-managing-data-as-file-object
Stringio And Bytesio For Managing Data As File Object - GeeksforGeeks
July 24, 2025 - StringIO and BytesIO are classes provided by the io module in Python. They allow you to treat strings and bytes respectively as file-like objects. This can be useful when you want to work with in-memory file-like objects without actually writing to or reading from physical files.
🌐
GitHub
github.com › python › cpython › blob › main › Modules › _io › bytesio.c
cpython/Modules/_io/bytesio.c at main · python/cpython
_io.BytesIO.seek · pos: Py_ssize_t · whence: int = 0 · / · Change stream position. · Seek to byte offset pos relative to position indicated by whence: 0 Start of stream (the default). pos should be >= 0; 1 Current position - pos may be negative; 2 End of stream - pos usually negative.
Author   python
🌐
Reddit
reddit.com › r/learnpython › vs
r/learnpython on Reddit: <class 'bytes'> vs <class '_io.BytesIO'>
December 1, 2022 -

Hello all,

I'm trying to wrap my head around the practical differences between:

<class 'bytes'> and <class '_io.BytesIO'>.

I read through the documentation:

https://docs.python.org/3/library/io.html?highlight=bytesio#binary-i-o

Binary I/O (also called buffered I/O) expects bytes-like objects and produces bytes objects. No encoding, decoding, or newline translation is performed. This category of streams can be used for all kinds of non-text data, and also when manual control over the handling of text data is desired.

It provides some examples:

The easiest way to create a binary stream is with open() with 'b' in the mode string:

and

f = io.BytesIO(b"some initial binary data: \x00\x01")

So I read all this, but so what? Why would you use the io.BytesIO data type over a standard bytes data type?

EDIT: Let me provide some additional context that I just discovered after reading the documentation on lxml.

https://lxml.de/parsing.html#parsing-html

I'm using the requests object and parsing the results with lxml. Here is the example code:

from io import BytesIO
from lxml import etree
#* etree - https://lxml.de/parsing.html
#? etree stands for element tree

import requests

#? Need to know concepts
#?  What are bytes
#?  HTTP status codes
#?  HTTP methods (GET. POST, PUT, DELETE)
#?  bytes - https://docs.python.org/3/library/stdtypes.html?highlight=bytes#bytes-objects

url = 'http://localhost'
#! The URL https://nostarch.com/ doesn't seem to work

resp = requests.get(url=url)
html_bytes = resp.content
parser = etree.HTMLParser()
content = etree.parse(BytesIO(html_bytes), parser=parser)

print(type(html_bytes))
print(type(BytesIO(html_bytes)))

for link in content.findall('//a'):
    print(f"{link.get('href')} -> {link.text}")

Kind regards

🌐
Code-learner
code-learner.com › home › python stringio and bytesio example
Python StringIO And BytesIO Example ·
March 21, 2021 - # Import BytesIO module. >>> from io import BytesIO # Create a BytesIO object. >>> bytIO = BytesIO() # Write bytes that are utf-8 encoded chine word. >>> bytIO.write('中文'.encode('utf-8')) 6 # Get the value and print. >>> print(bytIO.getvalue()) # The value is byte charactor not a string.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to io.bytesio
5 Best Ways to Convert Python Bytes to io.BytesIO - Be on the Right Side of Change
February 23, 2024 - This method is straightforward and the one you’d likely come across most often. It involves directly passing the bytes data to the constructor of io.BytesIO, which returns a stream object. ... This code snippet creates a bytes object named data, then initializes an io.BytesIO object with it.
🌐
ProgramCreek
programcreek.com › python › example › 1734 › io.BytesIO
Python Examples of io.BytesIO
def _serialize_data(self, data): # Default to raw bytes type_ = _BYTES if isinstance(data, np.ndarray): # When the data is a numpy array, use the more compact native # numpy format. buf = io.BytesIO() np.save(buf, data) data = buf.getvalue() type_ = _NUMPY elif not isinstance(data, (bytearray, bytes)): # Everything else except byte data is serialized in pickle format.
🌐
Learning Machine
gallon.me › using-the-bytesio-class-in-python.html
Using the BytesIO Class in Python - Learning Machine
February 14, 2025 - The io.BytesIO class in Python is an in-memory stream for binary data. It provides a file-like interface that lets you read and write bytes just like you would with a file, but all the data is...
🌐
Webkul
webkul.com › home › python: using stringio and bytesio for managing data as file object
Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog
October 1, 2019 - Till python2.7 we were using cStringIO or StringIO while dealing with these data steam.Now in Python 3.x, we are using io.StringIO or io.BytesIO from the io module, as the StringIO, and cStringIO modules are no longer available in Python 3.x. In Python 2.7 StringIO module was capable handling the Byte as well Unicode But in python3 you will have to use separate BytesIO for handling Byte strings and StringIO for handling Unicode strings.
🌐
Medium
medium.com › @sarthakshah1920 › harnessing-the-power-of-in-memory-buffers-with-bytesio-0ac6d5493178
Harnessing the Power of In-Memory Buffers with BytesIO | by Sarthak Shah | Medium
December 24, 2023 - We start by importing the BytesIO class from the io module. This class allows us to create an in-memory buffer that behaves like a file, providing read and write operations without the need for physical storage.