visionmedia/superagent — pull request #37 by jackyz (2011-12-10 13:24:07+00:00)
Title: found and fixed a bug while sending multipart/form-data request

I try to use express.bodyParser middleware (the old one of connect 1.8.1) on the serverside to parse the request that send by superagent. But no lucky, something goes wrong.

I found at the beginning of body, there is a extra \r\n cause formidable parse failure.

Here is my test code

the server side code.

```javascript
var util = require('util')
, express = require('express')
, bodyParser = require('./bodyParser');

var web = express.createServer();
web.configure(function(){
web.use(express.bodyParser({uploadDir: '/tmp', keepExtensions: true}));
web.use(web.router);
});

web.post('/_/echo', function(req, res, next){
console.log("req.head\n"+util.inspect(req.headers,false, null));
console.log("req.body\n"+util.inspect(req.body, false, null));
res.send(req.body);
});

web.listen(3005);
```

the client side code.

```javascript
var request = require("superagent")
, util = require("util")
, fs = require("fs");

var req = request.post('http://localhost:3005/_/echo');
req.type('multipart/form-data');

req.part()
.set('Content-Disposition', 'form-data; name="abc"')
.write("hello");

req.part()
.set('Content-Disposition', 'form-data; name="abc"')
.write("world");

var part3 = req.part();
part3.set('Content-Disposition', 'form-data; name="abc"; filename="blank.gif"');
part3.set('Content-Type', 'image/gif');
part3.write(fs.readFileSync(__dirname+"/blank.gif"));

// i don't know why but it just doesn't work as expect
// var fstream = fs.createReadStream(__dirname+"/blank.gif");
// fstream.pipe(part3);

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

the equally curl script

```bash
curl http://localhost:3005/_/echo -F abc="hello" -F abc="world" -F abc=@blank.gif
```

You can try these code to reproduce the problem, if you need.

Hope this help.