Getting following error while posting compressed payload from Java client (Unirest) to Flask Api.
[POST]>. Error 400 Bad Request: Failed to decode JSON object: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte while decompression. \",). in loads\n s = s.decode(encoding)\nUnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte\n\nDuring handling of the above exception, another exception occurred
The payload is passed in compressed format. The post request works fine from python test code.
I have following java post request.
public void PostData() throws Exception {
String payload = "{\"FirstName\": \"ABC\", \"LastName\": \"XYZ\"}";
ByteArrayOutputStream obj = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(obj);
gzip.write(payload.getBytes("UTF-8"));
gzip.close();
HttpResponse<JsonNode> jsonResponse
= Unirest.post("http://localhost:5000/service")
.header("Accept-Encoding", "gzip")
.body(obj.toByteArray())
.asJson();
}
Following python code is receiving request. modified code to make it bit simple. The code fails at .decode('utf8')
def post(self):
payload = json.loads(gzip.decompress(request.data).decode('utf8')) if request.content_encoding is 'gzip' else request.get_json('data')
return Response(payload, status=201)
Following is working pyhton test code
def test_post(self):
payload = {'FirstName': 'ABC', 'LastName': 'XYZ'}
payload = gzip.compress(json.dumps(payload).encode())
resp = self._client.post('/service', data=payload, headers={'Content-Encoding': 'gzip'})