I need to get a file from a URL (any file type) and base64 encode it, and then send it to an API that only accepts base64 encoded files. Here is the documentation description just for clarification:
The file must be base64-encoded. You should not use MIME style encoding with newline characters at a maximum line length of 76. Instead, you are required to omit these newlines. (If you don’t omit the newlines, the stored file won’t be usable.)
Below is the current code I am using. I am able to upload the file to the API in question, but the file has been corrupted for some reason. The current URL I am using for this is (just a dummy image for testing):
I am getting the file to upload to the API in question - but when opened it is corrupted. I am assuming it has something to do with the base64 restrictions noted above for the API. My code for the base64 encoding.
UPDATED - async accounted for and still the same issue:
const request = require('request');
function sendBase64EncodedFile( url ) {
request.get( url , function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = new Buffer(body).toString('base64');
/*
Create xml to send to API
*/
var xml = '<qdbapi><usertoken>hidden for privacy issues</usertoken><apptoken>hidden for privacy issues</apptoken><field fid="8">Some testing body text</field><field fid="9" filename="someFileName.png">' + data + '</field></qdbapi>';
var qb_url = "hidden for privacy issues";
request({
url: qb_url,
method: "POST",
'headers': {
'Content-Type': 'application/xml',
'QB-ACTION': 'API_AddRecord'
},
body: xml
}, function (error2, response2, body2){
});
} else {
//console.log( body );
}
});
}