Note: I forked this and made a bunch of changes. It should still be legacy compatible, but I make no promises.
Module allows to send POST requests with Content-type: multipart/form-data
with files and any readable streams.
npm install multiparter
-
var request = new multiparter.request(protocol, options)Create request object. For protocol you may pass
httporhttpsmodule,optionsis request options object just like forhttp.request. -
request.setParam(field, value)Set plain field value for your request.
-
request.addStream(field, fileName, mimeType, streamLength, stream)Add stream to request and assign it to specified field. Stream must have mime type (pass
application/octet-streamif unsure) and length to calculate request length without loading whole stream data into memory. -
request.send(callback)Send request and receive
erroras first argument of callback andresponseas second (usual http.ClientResponse or https.ClientResponse object)
var fs = require("fs");
var http = require("http");
var multiparter = require("multiparter");
// test host, but it works
var request = new multiparter.request(http, {
host: "bobrik.name",
port: 80,
path: "/test2.php",
method: "POST",
boundary: "MyBoundary"
});
// add some plain params here
request.setParam("here", "we are");
request.setParam("again", "ok bye!");
// add files you need with readable stream
// don't forget to crete file test.txt
request.addStream(
"myfile",
"test.txt",
"text/plain",
19,
fs.createReadStream("test.txt")
);
// send request and receive response
request.send(function(error, response) {
if (error) {
console.log(error);
}
var data = "";
response.setEncoding("utf8");
response.on("data", function(chunk) {
data += chunk;
});
response.on("end", function() {
console.log("Data: " + data);
});
response.on("error", function(error) {
console.log(error);
});
});- Ian Babrou (ibobrik@gmail.com)