I'm trying to get a picture [via Node http get request] from another Node app, using axios. The problem is that when I use axios I get bad format of the file and when I save it, it doesnt look lime a picture... When I use http get, I get a perfect picture and it is saved well.
- The working code to get the picture:
The Receiver:
var data = new Stream.Transform();
res.on('data', function(chunk) {
console.log(typeof chunk)
data.push(chunk);
});
res.on('end', function() {
fs.writeFileSync('event.png', data.read());
});
The Sender:
export const getImage = (req, res) => {
const pathToImage = 'C:/dev/server/images/event.jpg';
const img = fs.readFileSync(pathToImage);
res.writeHead(200, {'Content-Type': 'blob' });
res.end(img, 'binary');
}
- The axios (not working code):
axios.get(url)
.then((response: AxiosResponse)=>{
// save picture locally:
let data = new Stream.Transform();
data.push(response.data);
fs.writeFileSync('event.jpg', data.read());
})
.catch((error: AxiosError) => {
console.log(error.message);
})
Is there any idea why the axios doesn't work like the http ?