Zlib.error: error – 3 while decompressing: incorrect header check in Python

As shown in the following code:

b64_encoded_bytes = base64.b64encode(zlib.compress(b'abcde'))
encoded_bytes_representation = str(b64_encoded_bytes)  # this the cause

Zlib.decopress error

zlib.error: Error -3 while decompressing: incorrect header check

The reason for this is the use of the str() method
The description of the str() method in the python3 documentation.
If neither encoding nor errors is given, str(object) returns object.str(), which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a str() method, then str() falls back to returning repr(object).
The str() method only serves to put the


Converted to


The correct conversion should be to


The solution is to specify the encoding method in the str() method

str(b64_encoded_bytes, 'utf-8')

Read More: