Node.js download PDF file from REST Api

Last Updated: March 21, 2021

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('', {
            method: 'GET',
            responseType: "stream"
        .then(response => { 
        .catch(error => {

Once you get the response from the server you can use to create the stream to write the file contents. So you can use the fs.createWriteStream("data.pdf") to achieve that