How to get binary content in Node.js using request?

Sometimes, we want to get binary content in Node.js using request.

In this article, we’ll look at how to get binary content in Node.js using request.

How to get binary content in Node.js using request?

To get binary content in Node.js using request, we should set the encoding setting to null.

For instance, we write

const requestSettings = {
  method: 'GET',
  url,
  encoding: null
};

request(requestSettings, (error, response, body) => {
  // ...
})

to call request with requestSettings and a callback that’s run when the request is done.

We set encoding to null so that the response body in the callback would give us the binary data from the response.

Conclusion

To get binary content in Node.js using request, we should set the encoding setting to null.