title: Set node request header based on encoded formData value
body: <p>I'm attempting to send an HTTP POST, using the <code>request</code> npm package, to an API that requires a special header, consisting of a base64 encoded MD5 hash of the request body's string representation.</p>
  
  <p><strong>Code to generate the MD5 hash:</strong></p>
  
  <pre><code>function md5(val) {
      val = val || '';
      return crypto.createHash('md5').update(val).digest('base64');
  }
  </code></pre>
  
  <p>When there's no formData, for a GET or DELETE request, the value I'm supposed to use is an empty string, which works great. The API accepts the header and returns the requested data.</p>
  
  <p>Unfortunately, when posting a file, using the <code>formData</code> option, the object is being encoded by the <code>request</code> module. So, when the server compares my MD5 hash with the body that's received on their end, it doesn't match and throws an error. </p>
  
  <p><strong>Simplified request of what I need:</strong></p>
  
  <pre><code>var formData = {
      left: 0,
      top: 0,
      width: 0,
      height: 0,
      profileImage: fs.readFileSync(__dirname + '/test_image.jpg')
  };
  
  var reqOptions = {
      url: 'https://example.com/user/1234/profile-image',
      method: 'POST,
      json: true,
      headers: {
          'Content-MD5': md5(formData)
      },
      formData: formData
  }
  
  request(reqOptions, function(err, response, body) {
      //process the response...
  });
  </code></pre>
  
  <p>The example above will throw an error, because the formData variable is an object and the <code>crypto</code> module is expecting a string. I started to manually write code to convert the formData object to a string, but it seems a little ridiculous to rewrite all of the logic that encodes all of the form values when it's already being done by the <code>request</code> module.</p>
  
  <p>I'm looking for a reliable way to get the <strong>exact</strong> encoded form contents, after they've been processed by the <code>request</code> module, but before the request is actually sent, so I can build the hash value and add the header.</p>