Search code examples
pythonpython-3.xtypeerrorzlibbytesio

Convert from '_io.BytesIO' to a bytes-like object in python3.6?


I am using this function to uncompress the body of a HTTP response if it is compressed with gzip, compress or deflate.

def uncompress_body(self, compression_type, body):
    if compression_type == 'gzip' or compression_type == 'compress':
        return zlib.decompress(body)
    elif compression_type == 'deflate':
        compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
        compressed = compressor.compress(body)
        compressed += compressor.flush()
        return base64.b64encode(compressed)

    return body

However python throws this error message.

TypeError: a bytes-like object is required, not '_io.BytesIO'

on this line:

return zlib.decompress(body)

Essentially, how do I convert from '_io.BytesIO' to a bytes-like object?


Solution

  • 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.