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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With