title: NodeJS Request error sending multipart/form-data
body: <p>I'm using a Node.js app as proxy for a webservice. I'm able to transfer simple application/json request with body-parser but it cant handle multipart/form-data.</p>
  
  <p>To do so, I'm trying to use the <a href="https://www.npmjs.com/package/request" rel="nofollow">request</a> module but I have an error when I attach files to the request : </p>
  
  <pre>
  Error: write after end
      at ClientRequest.OutgoingMessage.write (_http_outgoing.js:413:15)
      at Request.write(./node_modules/request/request.js:1362:25)
  </pre>
  
  <p>Here's the code that triggers this error :</p>
  
  <pre><code>var form_data = {};
  
              for (var key in req.files){
                  form_data[req.files[key].fieldname] = fs.createReadStream(req.files[key].path);
                  log.debug("File :", req.files[key].fieldname)
              }
  
              var multipart_request = request_module({
                  url: 'http://example.com',
                  method: ‘POST’,
                  headers: req.headers,
                  body: JSON.stringify({&lt;some content here&gt;}),
                  formData: form_data
              }, function(error, response, body){
  
                  if(error){
                      log.debug(error);
                      res.status(500).send('{"message": "Server error.}');
                  }
                  else{
                      res.status(response.statusCode).send(body);
                  }
  
              });
  </code></pre>
  
  <p>Is an other solution to do that ?</p>