tornado web application fails to decode compressed http body
<p>I am writing some simple prototype tornado web applications and found that tornado fails to decode the http request body with</p>

<pre><code>Error -3 while decompressing: incorrect header check
</code></pre>

<p>From one of the tornado web application, I am sending http request by compressing the body using zlib.</p>

<pre><code>http_body = zlib.compress(data)
</code></pre>

<p>And also added http header:</p>

<pre><code>'Content-Encoding': 'gzip' 
</code></pre>

<p>However, when I receive this http request in another tornado web application, I see that it results in decompressing failure as mentioned above.</p>

<p>Sample Code to handle the http request: </p>

<pre><code>class MyRequestHandler(tornado.web.RequestHandler):
    def post(self):
        global num
        message = self.request.body
        self.set_status(200)
</code></pre>

<p>I have also ensured that <strong>decompress_request=True</strong> when application listen.</p>

<p>I have checked the tornado documentation and earlier post and found nothing regarding compressed http body part or any example of it. The only thing mentioned is decompress_response parameter which just ensures compressed http response from a server.</p>

<p>Am I missing any settings here?</p>

<p><code>gzip</code> and <code>zlib</code> are both based on the same underlying compression algorithm, but they are not the same thing. You must use <code>gzip</code> and not just <code>zlib</code> here:</p>

<pre><code>def post_gzip(self, body):
    bytesio = BytesIO()
    gzip_file = gzip.GzipFile(mode='w', fileobj=bytesio)
    gzip_file.write(utf8(body))
    gzip_file.close()
    compressed_body = bytesio.getvalue()
    return self.fetch('/', method='POST', body=compressed_body,
                      headers={'Content-Encoding': 'gzip'})
</code></pre>

<p>Some <code>zlib</code> functions also take cryptic options that cause them to produce <code>gzip</code>-format output. These can be used with <code>zlib.decompressobj</code> and <code>zlib.compressobj</code> to do streaming compression and decompression.</p>

python http tornado