I want to download pdf file from the server with REST API. This is has to be done inside my node.js application
This can be done easily with axios
.
First you have to send the get request to server with responseType: "stream"
If you need to do authentication you can pass the acessToken with following line
axios.defaults.headers.common['Authorization'] = 'Bearer '+arg.acessToken
axios.defaults.headers.common['Authorization'] = 'Bearer '+ arg.acessToken
axios('http://127.0.0.1:8000/api/pdf/invoice/65', {
method: 'GET',
responseType: "stream"
})
.then(response => {
response.data.pipe(fs.createWriteStream("data.pdf"));
})
.catch(error => {
console.log(error)
});
Once you get the response from the server you can use response.data.pipe
to create the stream to write the file contents. So you can use the fs.createWriteStream("data.pdf")
to achieve that