Skip to content

Instantly share code, notes, and snippets.

@wuchengwei
Created December 26, 2011 08:34
Show Gist options
  • Save wuchengwei/1520763 to your computer and use it in GitHub Desktop.
Save wuchengwei/1520763 to your computer and use it in GitHub Desktop.
NodeJS - Http Post
//doHttpPost('localhost', 8000, '/TestPost', 'string' , 'TestTestTestTest', false);
//doHttpPost('localhost', 8000, '/TestPost', 'file' , '/Users/chengwei/Downloads/grid1.png', true);
function doHttpPost(_host, _port, _path, name, value, isFile, fileEncoding) {
var http = require('http'),
fs = require('fs'),
path = require('path'),
boundary = Math.random(),
postData, postOptions, postRequest;
if (isFile) {
var fileName = path.basename(value),
encoding = fileEncoding?fileEncoding:'binary',
dataLength = 0,
fileContents = '',
fileReader;
postData = [];
postData.push(new Buffer(EncodeFilePart(boundary, 'application/octet-stream', name, fileName), 'ascii'));
fileReader = fs.createReadStream(value, {'encoding': encoding});
fileReader.on('data', function(data) {
fileContents += data;
});
fileReader.on('end', function() {
postData.push(new Buffer(fileContents, encoding));
postData.push(new Buffer("\r\n--" + boundary + "--"), 'ascii');
// Calculate data length
for(var i = 0; i < postData.length; i++) {
dataLength += postData[i].length;
}
postOptions = {
'host': _host,
'port': _port,
'path': _path,
'method': 'POST',
'headers' : {
'Content-Type' : 'multipart/form-data; boundary=' + boundary,
'Content-Length' : dataLength
}
};
postRequest = http.request(postOptions, function(response){
response.setEncoding('utf8');
response.on('data', function(chunk) {
// Respond to http response
console.log(chunk);
});
response.on('end', function() {
// Respond to http response
console.log('send ' + value);
});
});
for (var i = 0; i < postData.length; i++) {
postRequest.write(postData[i]);
}
postRequest.end();
});
}
else {
postData = '';
postData += EncodeFieldPart(boundary, name, value);
postData += '--' + boundary + '--';
postOptions = {
'host': _host,
'port': _port,
'path': _path,
'method': 'POST',
'headers' : {
'Content-Type' : 'multipart/form-data; boundary=' + boundary,
'Content-Length' : postData.length
}
};
postRequest = http.request(postOptions, function(response){
response.setEncoding('utf8');
response.on('data', function(chunk) {
// Respond to http response
console.log(chunk);
});
response.on('end', function() {
// Respond to http response
console.log('send ' + value);
});
});
postRequest.write(postData);
postRequest.end();
}
function EncodeFieldPart(boundary,name,value) {
var return_part = "--" + boundary + "\r\n";
return_part += "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
return_part += value + "\r\n";
return return_part;
}
function EncodeFilePart(boundary,type,name,filename) {
var return_part = "--" + boundary + "\r\n";
return_part += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n";
return_part += "Content-Type: " + type + "\r\n\r\n";
return return_part;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment