lundi 2 mars 2015

Emulating form upload with node.js

I'm trying to emulate a form upload with node.js. The reason is because I'm trying to automate some existing code. My node script looks like this:



var boundary = '--123spamulator--'

var options = {
hostname: 'myawesomewebsite.com',
port: 80,
path: '/api/UploadHandler.ashx',
method: "POST",
headers: {
'Content-Type': 'multipart/form-data; boundary=' + boundary,
}
};

var req = http.request(options, function (res) {
var data = "";
res.on("data", function (chunk) {
data += chunk;
});
res.on("end", function (r) {
console.log("END");
//callback();
});
res.on("error", function (err) {
console.log("POSTERROR (image)");
console.log(err);
});
});

req.on("error", function (err) {
console.log("POSTERROR - 2 (image)");
console.log(err);
});

var idPart = "\r\n";
idPart += "Content-Disposition: form-data; name='ID'";
idPart += "\r\n\r\n";
idPart += 12345;
idPart += "\r\n" + boundary;

console.log("SENDING:\r\n" + idPart);
req.write(idPart, "utf-8");
req.end();


and the request that gets sent looks like this:



Content-Disposition: form-data; name="Id"

12345
--123spamulator--


But when I try to read ID from my ASP.NET application like so:


var id = context.Request["ID"]


I get null.


Aucun commentaire:

Enregistrer un commentaire