Node.js Gzip inflating - node-compress

时间:2022-02-22 20:00:38

I'm trying to pull a gzipped json file from http://api.discogs.com/release/339901.

我正试图从http://api.discogs.com/release/339901中提取一个gzipped json文件。

I have https://github.com/waveto/node-compress installed and all works fine if I make a request to the Stack Overflow API, but once I try to request discogs I get an error.

我安装了https://github.com/waveto/node-compress,如果我向Stack Overflow API发出请求,一切正常,但是一旦我尝试请求discogs,我就会收到错误。

Assertion failed: (ret != Z_STREAM_ERROR), function GunzipInflate, file ../compress.cc, line 271.
Abort trap: 6

Code:

码:

    var options = {
        host: 'api.discogs.com',
        port: 80,
        path: '/release/339901',
        headers: {
            "Accept-Encoding": "gzip"
        }
    };

    var options = {
        host: 'api.*.com',
        port: 80,
        path: '/1.1/questions',
        headers: {
            "Accept-Encoding": "gzip"
        }
    }

    http.get(options, function(res){

        var body = [];
        var gunzip = new compress.Gunzip();
        gunzip.init();

        res.setEncoding('binary');

        res.on('data', function(chunk){
            body.push(gunzip.inflate(chunk, 'binary'));
        });

        res.on('end', function(){
            gunzip.end();
            callback(null, JSON.parse(body.join('')), response);
        });

        res.on('error', function(e){
            callback(e, null, response);
        });

    });



function callback(err, data, res) {
    if(err) {
        console.log(err);
    }
    else {
        res.end(JSON.stringify(data));
    }
}

Any ideas?

有任何想法吗?

UPDATE:

更新:

Seems they were not sending it gzipped. Here is what I eventually used.

似乎他们没有发送它gzipped。这是我最终使用的。

    var options = {
        host: 'api.discogs.com',
        port: 80,
        path: '/release/339901',
        headers: {
            "Accept-Encoding": "gzip"
        }
    };

    http.get(options, function(getRes){
        var body = "";

        getRes.on('data', function(chunk){
            body = body + chunk;
        });

        getRes.on('end', function(err, data){
            res.end(body);
        });
    });

1 个解决方案

#1


3  

It seems that api.discogs.com is not returning a gzip encoded response.

似乎api.discogs.com没有返回gzip编码的响应。

You should check the content-encoding header first:

您应该首先检查内容编码标头:

if (res.headers['content-encoding'] === 'gzip') { ... }

Asking for a gzip encoded response ("Accept-Encoding": "gzip") doesn't guarantee it.

要求gzip编码的响应(“Accept-Encoding”:“gzip”)并不能保证它。

You can verify it this way:

您可以这样验证:

console.log(JSON.stringify(res.headers));

res.on('data', function(chunk){
    console.log(chunk.toString());
});

#1


3  

It seems that api.discogs.com is not returning a gzip encoded response.

似乎api.discogs.com没有返回gzip编码的响应。

You should check the content-encoding header first:

您应该首先检查内容编码标头:

if (res.headers['content-encoding'] === 'gzip') { ... }

Asking for a gzip encoded response ("Accept-Encoding": "gzip") doesn't guarantee it.

要求gzip编码的响应(“Accept-Encoding”:“gzip”)并不能保证它。

You can verify it this way:

您可以这样验证:

console.log(JSON.stringify(res.headers));

res.on('data', function(chunk){
    console.log(chunk.toString());
});