There's a direct download link that downloads a PDF on a GET request. When I make a get request using a REST client like the postman, a binary data is received in the response. I want to convert this data to base64. Now, this base64 string needs to be passed to other API which in turn sends the file to WhatsApp chat. I successfully receive the document in WhatsApp but it is totally blank. The file size is the same as the source file, the same with the number of pages too.
var axios = require('axios');
let base64;
var config = {
method: 'get',
encoding: null,
url: '.................',
};
axios(config)
.then(function (response) {
// console.log(response.data, "-----------------------------------\n");
console.log(typeof response.data);
// base64 = Buffer.from(response.data).toString('base64');
// base64 = Buffer.from(response.data).toString('base64');
// base64 = new Buffer(response.data, "binary");
// base64 = Buffer.concat(response.data);
// base64 = new Buffer(response.data, "base64");
base64 = response.data.toString('base64');
var data = {...........};
var config2 = {
method: 'post',
url: '...........',
headers: {
'Content-Type': 'application/json',
'x-auth-token': '...........',
'Host': '.............'
},
data : data
};
axios(config2)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log("error while sending");
});
})
.catch(function (error) {
console.log(error, "error while requesting for the file");
});
I have tried the below solutions, none of them worked for me.