Receiving gzip with Flask

For Python 3, I would just use gzip.decompress( which returns a decompressed string.

It's just a convenient shorthand function, added 8 years ago :)

If you want to take a look at the code, you can find it here.

2019 edit: wrote a simple flask extension you can use in your app.

You import StringIO but never actually utilize it and feed a string to which requires a filename. The error you're getting is from gzip trying to decode the filename to Unicode before attempting to open it. The following utilizes StringIO to make a file-like object that can be used by gzip:

fakefile = StringIO.StringIO( # fakefile is now a file-like object thta can be passed to gzip.GzipFile:
uncompressed = gzip.GzipFile(fileobj=fakefile, mode='r')

Edit: I've refactored the code below and put relevant comments for a better understanding of what is going on:

from flask import Flask, request
import gzip, StringIO

app = Flask(__name__)

@app.route('/', methods = ['POST'])
def my_function():

    # `` is a compressed string and `gzip.GzipFile`
    # doesn't work on strings. We use StringIO to make it look
    # like a file with this:
    fakefile = StringIO.StringIO(

    # Now we can load the compressed 'file' into the 
    # `uncompressed` variable. While we're at it, we
    # tell gzip.GzipFile to use the 'rb' mode
    uncompressed = gzip.GzipFile(fileobj=fakefile, mode='rb')

    # Since StringIOs aren't real files, you don't have to 
    # close the file. This means that it's safe to return
    # its contents directly:

if __name__ == "__main__":
    app.debug = True

The accepted answer is correct for Python 2, but just in case you're trying this with Python 3, you need to use BytesIO instead of StringIO:

compressed_data = io.BytesIO(
text_data = gzip.GzipFile(fileobj=compressed_data, mode='r')