title: how to upload a file from node.js
body: <p>I found many posts when I queried for this problem, but they all refer to how to upload a file from your browser to a node.js server. I want to upload a file from node.js code to another server. I tried to write it based on my limited knowledge of node.js, but it doesn't work. </p>
  
  <pre><code>function (data) {
    var reqdata = 'file='+data;
    var request = http.request({
      host : HOST_NAME,
      port : HOST_PORT,
      path : PATH,
      method : 'POST',
      headers : {
        'Content-Type' : 'multipart/form-data',
        'Content-Length' : reqdata.length
      }
    }, function (response) {
        var data = '';
        response.on('data', function(chunk) {
          data += chunk.toString();
        });
        response.on('end', function() {
          console.log(data);
        });
      });
  
    request.write(reqdata+'\r\n\r\n');
    request.end();
  })
  </code></pre>
  
  <p>The above function is called by other code that generates data.</p>
  
  <p>I tried to upload same data file using curl -F "file=@&lt;filepath&gt;" and the upload is successful. But my code fails. The server returns an application specific error which hints that the uploaded file was invalid/corrupt.</p>
  
  <p>I collected tcpdump data and analysed it in wireshark. The packet sent from my node.js code lacks the boundary required for the multipart data. I see this message in wireshark packet</p>
  
  <pre><code>The multipart dissector could not find the required boundary parameter.
  </code></pre>
  
  <p>Any idea how to accomplish this in node.js code?</p>