Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping requests using gaxios (or axios)

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);
like image 540
drmrbrewer Avatar asked Mar 04 '20 18:03

drmrbrewer


People also ask

Why we use Axios in node JS?

Axios is a promise based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.

Does Axios use request?

Axios can make a GET request to “get” data from a server API. The axios. get() method is used to make an HTTP get request. There are two parameters that must be passed to the Axios get() method.

What are Axios requests?

Axios is a promise-based HTTP library that lets developers make requests to either their own or a third-party server to fetch data. It offers different ways of making requests such as GET , POST , PUT/PATCH , and DELETE .

What is Axios package used for?

Axios: Axios is a Javascript library used to make HTTP requests from node. js or XMLHttpRequests from the browser and it supports the Promise API that is native to JS ES6. It can be used intercept HTTP requests and responses and enables client-side protection against XSRF. It also has the ability to cancel requests.


1 Answers

Install gaxios:

npm install gaxios

And then use request with the Readable type specified and with responseType set to 'stream'.

// script.ts

import { request } from 'gaxios';

(await(request<Readable>({
  url: 'https://some.data.com',
  responseType: 'stream'
}))
.data)
.on('error', function(err) {
    console.log(err);
})
.pipe(unzipper.Parse())
.on('entry', myEntryHandlerFunction);
// first-example.ts

import { request } from 'gaxios';

// pipe req to destination...
const readableA = (await request<Readable>({
  url: 'http://some.url.com',
  method: 'POST',
  data: req, // suppose `req` is a readable stream
  responseType: 'stream'
})).data;
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
});

Gaxios is a stable tool and is used in official Google API client libraries. It's based on the stable node-fetch. And it goes with TypeScript definitions out of the box. I switched to it from the deprecated request and from the plain node-fetch library.

like image 162
Viacheslav Dobromyslov Avatar answered Sep 27 '22 20:09

Viacheslav Dobromyslov