Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *