0

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'})
Rajan Sharma
  • 325
  • 1
  • 4
  • 11

1 Answers1

0

header was different in both Java and python test cases.

Java header is "Accept-Encoding", "gzip"

python header is 'Content-Encoding': 'gzip'

There is check in post method if request.content_encoding is 'gzip'. This condition was filing for Java code and causing issue. Made header same in both cases. The code works fine.

Rajan Sharma
  • 325
  • 1
  • 4
  • 11