【问题标题】:Decompressing gzip response body解压 gzip 响应体
【发布时间】:2023-03-27 15:58:01
【问题描述】:

尝试使用 zlib 解压缩 gzip 响应主体,如下所示:

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);
        });

这将返回以下标头和错误:

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)? }

【问题讨论】:

    标签: node.js gzip fetch-api zlib


    【解决方案1】:

    我今天遇到了类似的问题,这个答案帮助了我 How do I read the contents of a Node.js stream into a string variable?

    在您的示例中 res.body 是一个 Gunzip 流,您需要做的就是借助上述答案中的 streamToString 函数将其输出为字符串,例如 const bodyText = await streamToString(res.body);

    【讨论】:

      最近更新 更多