0

Trying to decompress a gzip response body using zlib like this:

fetch(url, {
            method: "POST",
            headers: {
                "x-api-token": "***"
            }
        }).then(res => {
            console.log("headers", res.headers);

            zlib.gunzip(res.body, (err, unzipped) => {
                if (err) {
                    console.log("zlib error", err);
                } else {
                    console.log(unzipped.toString());
                }
            });
        }).catch(error => {
            console.log("error", error);
        });

This returns the following header and error:

I20210406-02:23:21.195(7)? headers Headers {
I20210406-02:23:21.196(7)?   [Symbol(map)]: [Object: null prototype] {
I20210406-02:23:21.197(7)?     'x-powered-by': [ 'Express' ],
I20210406-02:23:21.197(7)?     vary: [ 'Origin, Accept-Encoding' ],
I20210406-02:23:21.197(7)?     'content-type': [ 'application/json; charset=utf-8' ],
I20210406-02:23:21.197(7)?     etag: [ 'W/"41e907-UnYu+6xuu9JumYlvg/ni3o3thc4"' ],
I20210406-02:23:21.197(7)?     'x-response-time': [ '1300.917ms' ],
I20210406-02:23:21.198(7)?     'content-encoding': [ 'gzip' ],
I20210406-02:23:21.198(7)?     date: [ 'Mon, 05 Apr 2021 19:23:22 GMT' ],
I20210406-02:23:21.198(7)?     via: [ '1.1 google' ],
I20210406-02:23:21.198(7)?     'alt-svc': [ 'clear' ],
I20210406-02:23:21.199(7)?     connection: [ 'close' ],
I20210406-02:23:21.199(7)?     'transfer-encoding': [ 'chunked' ]
I20210406-02:23:21.199(7)?   }
I20210406-02:23:21.199(7)? }
I20210406-02:23:21.199(7)? zlib error TypeError [ERR_INVALID_ARG_TYPE] [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Gunzip
I20210406-02:23:21.200(7)?     at validChunk (_stream_writable.js:281:10)
I20210406-02:23:21.200(7)?     at Gunzip.Writable.write (_stream_writable.js:316:21)
I20210406-02:23:21.200(7)?     at Gunzip.Writable.end (_stream_writable.js:585:10)
I20210406-02:23:21.200(7)?     at zlibBuffer (zlib.js:118:10)
I20210406-02:23:21.201(7)?     at Object.asyncBufferWrapper [as gunzip] (zlib.js:774:12)
I20210406-02:23:21.201(7)?     at imports/api/post.js:17:9
I20210406-02:23:21.201(7)?     at C:\Users\Dev\AppData\Local\.meteor\packages\promise\0.11.2\npm\node_modules\meteor-promise\fiber_pool.js:43:40 {
I20210406-02:23:21.202(7)?   code: 'ERR_INVALID_ARG_TYPE'
I20210406-02:23:21.202(7)? }
I20210406-02:23:21.203(7)? zlib error Error: unexpected end of file
I20210406-02:23:21.203(7)?     at Zlib.zlibOnError [as onerror] (zlib.js:182:17) {
I20210406-02:23:21.204(7)?   errno: -5,
I20210406-02:23:21.204(7)?   code: 'Z_BUF_ERROR'
I20210406-02:23:21.204(7)? }
Gravity123
  • 1,130
  • 3
  • 21
  • 39

1 Answers1

1

I've faced the similar problem today, this answer helped me How do I read the contents of a Node.js stream into a string variable?

In your example res.body is a Gunzip stream, all you need to do is to output it to string with the help of streamToString function from the answer above like const bodyText = await streamToString(res.body);

ayartsev
  • 60
  • 7