title: Node.js Download File Using Content Disposition as Filename
body: <p>I'm using the Request module to download files, but I'm not quite sure how to pipe the response to an output stream when the filename must come from the 'Content-Disposition' header. So basically, I need to read the response until the header is found, and then pipe the rest to that filename.</p>
  
  <p>The examples show something like:</p>
  
  <p><code>request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'));</code></p>
  
  <p>Where I want to do (pseudocode):</p>
  
  <pre><code>var req = request('http://example.com/download_latest_version?token=XXX');
  var filename = req.response.headers['Content-Disposition'];
  
  req.pipe(fs.createWriteStream(filename));
  </code></pre>
  
  <p>I could get the filename using the Request callback:</p>
  
  <pre><code>request(url, function(err, res, body) {
   // get res headers here
  });
  </code></pre>
  
  <p>But wouldn't that negate the benefits of using pipe and not loading the downloaded file into memory?</p>