Search code examples
c++qthttpgzipqnetworkaccessmanager

Read undecoded data from QNetworkReply


Is it possible to read undecoded data from QNetworkReply?

Response is encoded using gzip (Content-Encoding: gzip HTTP header), but when i call readAll() method it returns decoded data. I need raw gzipped data, as it was sent to me. Any ideas?


Solution

  • You have to set yourself the header for your QNetworkRequest:

     networkRequest.setRawHeader("Accept-Encoding", "gzip");
    

    Then Qt doesn't decode for you in the reply.

    We can see in the source of qhttpnetworkconnection.cpp for QHttpNetworkConnectionPrivate::prepareRequest:

        // If the request had a accept-encoding set, we better not mess
        // with it. If it was not set, we announce that we understand gzip
        // and remember this fact in request.d->autoDecompress so that
        // we can later decompress the HTTP reply if it has such an
        // encoding.
        value = request.headerField("accept-encoding");
        if (value.isEmpty()) {
    #ifndef QT_NO_COMPRESS
            request.setHeaderField("Accept-Encoding", "gzip");
            request.d->autoDecompress = true;
    #else
            // if zlib is not available set this to false always
            request.d->autoDecompress = false;
    #endif
        }