Categories
JavaScript Answers

How to ungzip (decompress) a Node.js request’s module gzip response body?

Spread the love

To ungzip (decompress) a Node.js request’s module gzip response body, we use the zlib library.

For instance, we write

const request = require("request");
const zlib = require("zlib");
const concat = require("concat-stream");

request(url)
  .pipe(zlib.createGunzip())
  .pipe(
    concat((stringBuffer) => {
      console.log(stringBuffer.toString());
    })
  );

to call request to make a get request to the file url.

Then we call pipe with the stream returned by zlib.createGunzip() to unzip the file.

And we call pipe again with the stream returned by the concat function to get the unzipped data as a buffer from stringBuffer.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *