Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving gzip with Flask

I'm trying to receive a gzipped JSON file from an HTTP POST to Flask (v0.10). I feel there may be some extra data posted that needs stripping out before trying to open the gzip.

Here's the code I have:

from flask import Flask, jsonify, request, abort
import gzip, StringIO
app = Flask(__name__)

# Handle posted data
@app.route('/', methods = ['POST'])
def post_gzip():

    # Check for a supported media type
    if (request.headers['Content-Type'] == 'application/x-gzip'):

        file = request.data
        f = gzip.open(file, 'rb')        

        return f;

    else:
        # 415 Unsupported Media Type
        abort(415)

if __name__ == "__main__":
    app.debug = True
    app.run()

I'm posting a zipped JSON file to Flask with cURL as follows:

curl -X POST -d @test.json.gz http://127.0.0.1:5000/ -H "Content-Type:application/x-gzip" -H "Content-Encoding:gzip"

And the error I'm receiving is:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte

It seems like Flask can't see the received data as being a gz file. Perhaps request.data isn't the right thing to use even.

Could some kind person point me in the right direction with this one?

like image 963
novabracket Avatar asked Feb 03 '15 16:02

novabracket


1 Answers

For Python 3, I would just use gzip.decompress(request.data) 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.

like image 136
ronlut Avatar answered Sep 19 '22 15:09

ronlut