how to load an image from url into buffer in nodejs

I was able to solve this only after reading that encoding: null is required and providing it as an parameter to request.

This will download the image from url and produce a buffer with the image data.

Using the request library -

const request = require('request');

let url = 'http://website.com/image.png';
request({ url, encoding: null }, (err, resp, buffer) => {
     // Use the buffer
     // buffer contains the image data
     // typeof buffer === 'object'
});

Note: omitting the encoding: null will result in an unusable string and not in a buffer. Buffer.from won't work correctly too.

This was tested with Node 8


Try setting up request like this:

var request = require('request').defaults({ encoding: null });
request.get(s3Url, function (err, res, body) {
      //process exif here
});

Setting encoding to null will cause request to output a buffer instead of a string.


import fetch from "node-fetch";

let fimg = await fetch(image.src)
let fimgb = Buffer.from(await fimg.arrayBuffer())

Use the axios:

const response = await axios.get(url,  { responseType: 'arraybuffer' })
const buffer = Buffer.from(response.data, "utf-8")