1

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):

dummy image

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 );
       }
    });

}
Jim M
  • 321
  • 2
  • 5
  • 12
  • 2
    base64EncodedFile will always return undefined. You might want to pass a callback argument into it to be called with the encoded data. – Gerrit0 Jan 23 '17 at 17:38
  • @Gerrit0 - I see what you mean. Even if I grab the output of "data" and manually set a variable to that value however - the file is still corrupted. For some reason this version of base64 encoding doesn't seem to jive with the API I am tying into. Only thought was it had something to do with the API restrictions mentioned above for base64 encoding. – Jim M Jan 23 '17 at 17:51
  • 1
    You don't seem to understand that because `request.get()` in your function is asynchronous, you cannot return the result from your function. You need to either return a promise and resolve the promise with your data or pass a callback into your function and call that callback with the data when you have it. Your data is only ready long, long after your function has already returned. See [How to return data from asynchronous function](http://stackoverflow.com/a/14220323/816620) for more info. – jfriend00 Jan 23 '17 at 17:57
  • It sounds to me like you might need to ask a question specifically about the API you are using then, as it looks like the problem is with the API. I'm not familiar with Quickbase (I assume you are using) and can't really help here. – Gerrit0 Jan 24 '17 at 15:17

0 Answers0