Skip to content

Getting response as stream #1

Answered by fin-kafka
wannacfuture asked this question in Q&A
Discussion options

You must be logged in to vote

Try this code snippet.

const axios = require('axios');
const fs = require('fs');

axios({
method: 'get',
url: 'https://your-url.com/path/to/stream',
responseType: 'stream',
})
.then((response) => {
// Handle the stream as needed, e.g., save it to a file
const writeStream = fs.createWriteStream('output-file.ext');
response.data.pipe(writeStream);

writeStream.on('finish', () => {
  console.log('File successfully downloaded and saved');
});

writeStream.on('error', (err) => {
  console.error('Error while saving the file:', err);
});

})
.catch((error) => {
console.error('Error while fetching the stream:', error);
});

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by wannacfuture
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants