Why is the downloaded image malformed?
import * as fs from 'fs';
import * as request from 'request-promise-native';
const download = async (url) => {
console.log(`Downloading ${url}`);
const options = {
url,
resolveWithFullResponse: true,
};
const response = await request.get(options);
console.dir(response.headers);
return fs.writeFileSync('image.jpg', response.body);
};
const main = async () => {
try {
await download('https://dz2cdn1.dzone.com/storage/rc-covers/3339976-refcard-cover141.png');
} catch (e) {
console.error(e);
}
};
main().then(() => console.log('success')).catch((e) => console.log(e));
The resulting image is malformed and can't be opened. Any ideas on what's causing the problem and how to fix it?