2

Due to the deprecation of request, we're currently rewriting the request-service in our node app with superagent. So far all looks fine, however we're not quite sure how to request binary data/octet-stream and to process the actual response body as a Buffer. According to the docs (on the client side) one should use

superAgentRequest.responseType('blob');

which seems to work fine on NodeJS, but I've also found this github issue where they use

superAgentRequest.buffer(true);

which works just as well. So I'm wondering what the preferred method to request binary data in NodeJS is?

eol
  • 23,236
  • 5
  • 46
  • 64

2 Answers2

4

According to superagent's source-code, using the responseType() method internally sets the buffer flag to true, i.e. the same as setting it manually to true.

In case of dealing with binary-data/octet-streams, a binary data parser is used, which is in fact just a simple buffer:

module.exports = (res, fn) => {
  const data = []; // Binary data needs binary storage

  res.on('data', chunk => {
    data.push(chunk);
  });
  res.on('end', () => {
    fn(null, Buffer.concat(data));
  });
};

In both cases this parser is used, which explains the behaviour. So you can go with either of the mentioned methods to deal with binary data/octet-streams.

eol
  • 23,236
  • 5
  • 46
  • 64
1

As per documentation https://visionmedia.github.io/superagent/

SuperAgent will parse known response-body data for you, currently supporting application/x-www-form-urlencoded, application/json, and multipart/form-data. You can setup automatic parsing for other response-body data as well:

You can set a custom parser (that takes precedence over built-in parsers) with the .buffer(true).parse(fn) method. If response buffering is not enabled (.buffer(false)) then the response event will be emitted without waiting for the body parser to finish, so response.body won't be available.

So to parse other response types, you will need to set .buffer(true).parse(fn). But if you do not want to parse response then no need to set buffer(true).

Vivek Patel
  • 1,028
  • 1
  • 10
  • 22
  • Thanks for your input - so if I just want to process the (complete) response-body as a binary buffer I actually don't have to do anything in Node? – eol May 30 '20 at 10:19
  • Of course, if you want to process response-body as binary then you will need to set response type to blob as you have done in option 1. Check the "Parsing response bodies" topic here: https://visionmedia.github.io/superagent, superagent handles these application/x-www-form-urlencoded, application/json, and multipart/form-data response types internally. But, if you want to handle other response types like Binary then here is a separate method: responseType('blob'). From my understanding, internally Superagent handling this way: buffer(true).parse(fn). Hope this helps – Vivek Patel May 30 '20 at 23:14
  • This still does not answer my question though ... the documentation says setting it to blob is only necessary for the web. And as I said using buffer(true) works perfectly fine. I think I'll have to examine the implementation to find the answer :) – eol May 31 '20 at 06:12