secret
Created

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 
var http = require('http'),
formidable = require('formidable'),
request = require('request'),
assert = require('assert'),
sys = require('sys');
 
var host = '127.0.0.1',
port = 1234;
 
var index = [
'<form action="/" method="post" enctype="multipart/form-data">',
' <input type="text" name="foo" />',
' <input type="submit" />',
'</form>'
].join("\n");
 
http.createServer(function(req, res) {
 
// Show a form for testing purposes.
if (req.method == 'GET') {
res.writeHead(200, {'content-type': 'text/html'});
res.end(index);
return;
}
 
// Parse form and write results to response.
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write(JSON.stringify({err: err, fields: fields, files: files}));
res.end();
});
 
}).listen(port, host, function() {
 
console.log("Server up and running...");
 
var server = this,
url = 'http://' + host + ':' + port;
 
var parts = [
{'Content-Disposition': 'form-data; name="foo"', 'body': 'bar'}
]
 
var req = request({method: 'POST', url: url, multipart: parts}, function(e, res, body) {
var obj = JSON.parse(body);
assert.equal("bar", obj.fields.foo);
server.close();
});
 
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.