title: Nodejs - Handle and send multipart request,
body: <p>My application runs on Nodejs server and Nodejs also acts as a middle ware/proxy for requests originating from the application. So from browser all REST calls goes to NodeJs, and then to Java API.</p>
  
  <p>I see an issue handling requests with multipart-form data.</p>
  
  <p>I intercept the file upload REST call from browser in my nodejs, parse the request using multiparty library, and form a form-data object from the file upload request.</p>
  
  <p>I am using https module to send data to API, so how do I send the form data request to API, via https ?</p>
  
  <p>I send  the Content-Type as multipart/form-data; boundary=----WebKitFormBoundary6fyv95baqEpoGJaK, got from browser.</p>
  
  <pre><code>var https = require('https');
  var multiparty = require('multiparty');
  var FormData = require('form-data');
  app.post('/v1/filesUpload', (request, response) =&gt; {
  let apiOptions ={
      headers: {
       'Content-Type': request.headers['Content-Type'],
        'host' : ...
        'path': ...    
       .
       .
       .
        }
  }
  let form = new multiparty.Form();
  let formdataReq = new FormData();
   Object.keys(fields).forEach(function (name) {
            console.log('got field named ' + fields[name]);
            formdataReq.append(name, fields[name].toString());
          });
  
          formdataReq.append('file', JSON.stringify(files));    
          const req = https.request(apiOptions, (res) =&gt; {
  
          });    
          req.write(querystring.stringify(formDataReq));
        }catch (e) {
          console.log(e);
        }
      });
  
  });
  </code></pre>