3

Using request module, I am trying to fetch response from a web service which has following header in the API request:

accept-encoding : gzip

and correspondingly, following header in the response :

content-encoding : gzip

When I am trying to decompress the response(get the correct readable response) using zlib(referred here), I am unable to do so.


Code Snippet :

var options = {
        url: url,
        qs: params.qparams,
        method: params.method,
        json: params.body,
        headers: {
            'api_key': configkey,
            'Content-Type': 'application/json',
            'Accept-Encoding': 'gzip'
        },
        timeout: constants.request_timeout
    };
request(options, function(err, response, body) {

        var encoding = response.headers['content-encoding']
        if (encoding && encoding.indexOf('gzip') >= 0) {
          zlib.gunzip(body, function(err, dezipped) {
            //ERROR : { [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }
            var json_string = dezipped.toString('utf-8');
            var json = JSON.parse(json_string);
            console.log('\nJSON ::\n',json);
          });
        } else {
            console.log('\n\nRESPONSE IS NOT GZIPPED!');
        }
}   

I am getting an error here(as commented in the code), using zlib.

I could not figure out as where is it going wrong, tried with multiple npm modules like unzipResponse and compress-buffer and tried different approaches as well as suggested at various places for handling gzip.

If someone can help out in resolving this, I'll be really thankful.

1 Answer 1

4

I have got a solution as need to add one more key to the options object as :

var options = {
    url: url,
    qs: params.qparams,
    method: params.method,
    json: params.body,
    headers: {
        'api_key': configkey,
        'Content-Type': 'application/json',
        'Accept-Encoding': 'gzip'
    },
    timeout: constants.request_timeout,
    encoding: null 
};

If someone has a better approach to perform the decompression, please add-on.

Sign up to request clarification or add additional context in comments.

1 Comment

I was using node-wget and getting similar issues for a certain URL. I switched to request and adopted your pattern and then it works. I wish I knew what exactly is going on here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.