I have an image on a web server (http://example.com/img.jpg). I open that image in a browser and save it to disk.
If I open the file in node via the ‘fs‘ module (fs.readFileSync), I get a Buffer that begins with 0xff, which is what I would expect.
I’d like to be able to get the same result directly from an HTTP request. I’m using the ‘request’ module to make the request.
request('http://example.com/img.jpg',function(error, response, body){
//code here
});
I can’t figure out how to turn the response or body into an equivalent Buffer from what I’m getting from the FileSystem. What am I missing?
You can get a
Bufferby setting theencodingtonull:requesttreats any other value as an argument forbuffer.toString(), which defaultsundefinedto"utf8".