Upload Progress — Request

I spent a couple of hours to find anything valid in request and node sources, and finally found a different approach, which feels more correct to me.

We can rely on drain event and bytesWritten property:

request.put({
  url: 'https://example.org/api/upload',
  body: fs.createReadStream(path)
}).on('drain', () => {
  console.log(req.req.connection.bytesWritten);
});

Alternatively if you need to handle progress of file bytes, it's easier to use stream data event:

let size = fs.lstatSync(path).size;
let bytes = 0;

request.put({
  url: 'https://example.org/api/upload',
  body: fs.createReadStream(path).on('data', (chunk) => {
    console.log(bytes += chunk.length, size);
  })
});

Stream buffer size is 65536 bytes and read/drain procedure runs iteratively.

This seems to be working pretty well for me with node v4.5.0 and request v2.74.0.


I needed a handle on the upload progress for yet another project of mine.

What I found out is that you can poll the request's connection._bytesDispatched property.

For example:

r = request.post url: "http://foo.com", body: fileAsBuffer
setInterval (-> console.log "Uploaded: #{r.req.connection._bytesDispatched}"), 250

Note: If you were piping to r, poll r.req.connection.socket._bytesDispatched instead.