visionmedia/superagent — pull request #374 by TooTallNate (2014-04-29 23:30:07+00:00)
Title: Use "form-data" module for the multipart/form-data implementation

This moves the `multipart/form-data` logic into a 3rd part module, [`form-data`](https://www.npmjs.org/package/form-data).

Multiple benefits arise with this, but most of all is much better handling of the `Request#field()` function, which is now very generic and can accept a String, Buffer, or fs.ReadStream instance as its _value_. This is important because it parallels the [client-side version of the same function](https://github.com/TooTallNate/superagent/commit/9cdb980c225b771c6d44e15d1946aa4229260c91).

I was doing a little test script like this which works as expected now:

``` js

var fs = require('fs');
var superagent = require('superagent');

var req = superagent.post('http://localhost:1024/upload')
.field('my_field', 'my value')
.field('my_buffer', new Buffer('hello world?'))
.field('my_file', fs.createReadStream(__filename));

// create a (deprecated) Part instance manually
var part = req.part()
.attachment('my_part', 'test.txt');

fs.createReadStream(__filename).pipe(part);

req.end(function (err, res) {
if (err) throw err;
console.log(res);
});
```

__p.s.__ I believe this is the last of my "refactoring" for now. Can we publish a new version to npm once this gets merged? Cheers!