Piping requests using gaxios (or axios)

drmrbrewer picture drmrbrewer · Mar 4, 2020 · Viewed 8.4k times · Source

At present I'm performing the trick of piping a request req to a destination url, and piping the response back to res, like so:

const request = require('request');

const url = 'http://some.url.com' + req.originalUrl;
const destination = request(url);

// pipe req to destination...
const readableA = req.pipe(destination);
readableA.on('end', function () {
    // do optional stuff on end
});

// pipe response to res...
const readableB = readableA.pipe(res);
readableB.on('end', function () {
    // do optional stuff on end
});

Since request is now officially deprecated (boo hoo), is this trick at all possible using the gaxios library? I thought that setting responseType: 'stream' on the request would do something similar as above, but it doesn't seem to work.

SImilarly, can gaxios be used in the following context:

request
.get('https://some.data.com')
.on('error', function(err) {
    console.log(err);
})
.pipe(unzipper.Parse())
.on('entry', myEntryHandlerFunction);

Answer

Ashish Modi picture Ashish Modi · Mar 7, 2020

I guess if you provide responseType as stream and use res.data, you will get a stream which you could pipe like this

const {request} = require("gaxios");
const fs = require("fs");
const {createGzip} = require("zlib");
const gzip = createGzip();

(async () => {
  const res = await request({
    "url": "https://www.googleapis.com/discovery/v1/apis/",
    "responseType": "stream"
  });
  const fileStream = fs.createWriteStream("./input.json.gz");
  res.data.pipe(gzip).pipe(fileStream);
})();