title: Nodejs post base64 string as form data
body: <p>I am currently receiving a file encoded as a base64 string as part of a json payload:</p>
  
  <pre><code>{
      "file":"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGRlZmluaXRpb25zIHhtb..."
  }
  </code></pre>
  
  <p>With that string I am supposed to post the file as multipart/form-data to a different service so I have a method like this (using <a href="https://github.com/mikeal/request" rel="noreferrer">request module</a>):</p>
  
  <pre><code>var request = require('request');
  var fs = require('fs');
  
  var importFile = function(fileBase64Encoded, cb) {
      var decodedFile = new Buffer(fileBase64Encoded, 'base64');
      var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
          if (err) {
              cb(err);
          }
          cb(null, body);
      });
      var form = r.form();
      form.append('file', decodedFile);
  }
  </code></pre>
  
  <p>And this is currently not working.
  If I write file to disk and read it from there like this:</p>
  
  <pre><code>var request = require('request');
  var fs = require('fs');
  
  var importFile function(fileBase64Encoded, cb) {
  
      var decodedFile = new Buffer(fileBase64Encoded, 'base64');
      fs.writeFile('temp.txt', decodedFile, function (err) {
          if (err) return console.log(err);
          var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
              if (err) {
                  cb(err);
              }
              cb(null, body);
          })
          var form = r.form();
          form.append('file', fs.createReadStream('temp.txt'));
      });
  
  }
  </code></pre>
  
  <p>Then it works...so Is there a real way to pass the base64 string as a valid parameter to the form? (right now trying with a buffer and not working)</p>