Skip to content

Commit

Permalink
allow passing in buffers as multipart bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer committed Oct 5, 2011
1 parent 646c80d commit d517ac0
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,29 +178,42 @@ Request.prototype.request = function () {
}

} else if (self.multipart) {
self.body = ''
self.body = [];
self.headers['content-type'] = 'multipart/related;boundary="frontier"'
if (!self.multipart.forEach) throw new Error('Argument error, options.multipart.')

self.multipart.forEach(function (part) {
var body = part.body
if(!body) throw Error('Body attribute missing in multipart.')
delete part.body
self.body += '--frontier\r\n'
var preamble = '--frontier\r\n'
Object.keys(part).forEach(function(key){
self.body += key + ': ' + part[key] + '\r\n'
preamble += key + ': ' + part[key] + '\r\n'
})
self.body += '\r\n' + body + '\r\n'
preamble += '\r\n';
self.body.push(new Buffer(preamble));
self.body.push(new Buffer(body));
self.body.push(new Buffer('\r\n'));
})
self.body += '--frontier--'
self.body.push(new Buffer('--frontier--'));
}

if (self.body) {
var length = 0;
if (!Buffer.isBuffer(self.body)) {
self.body = new Buffer(self.body)
if (Array.isArray(self.body)) {
for (var i = 0; i < self.body.length; i++) {
length += self.body[i].length;
}
} else {
self.body = new Buffer(self.body)
length = self.body.length;
}
} else {
length = self.body.length;
}
if (self.body.length) {
self.headers['content-length'] = self.body.length
if (length) {
self.headers['content-length'] = length;
} else {
throw new Error('Argument error, options.body.')
}
Expand Down Expand Up @@ -382,7 +395,13 @@ Request.prototype.request = function () {

process.nextTick(function () {
if (self.body) {
self.write(self.body)
if (Array.isArray(self.body)) {
self.body.forEach(function(part) {
self.write(part);
});
} else {
self.write(self.body)
}
self.end()
} else if (self.requestBodyStream) {
console.warn("options.requestBodyStream is deprecated, please pass the request object to stream.pipe.")
Expand Down

0 comments on commit d517ac0

Please sign in to comment.