Created
November 28, 2011 12:13
-
-
Save kitek/1400187 to your computer and use it in GitHub Desktop.
NODE.JS Wysyłanie HTTP POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
exports.sendPost = function(options,data,callback) { | |
var post_data = querystring.stringify(data); | |
var opts = { | |
host: options.host, | |
port: options.port, | |
maxSockets : 100, | |
path: options.path, | |
method: 'POST', | |
headers: { | |
'Host': options.host_name, | |
'Connection' : 'close', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': post_data.length | |
} | |
}; | |
http.globalAgent.maxSockets = 100; | |
var req = http.request(opts, function(res) { | |
var data = ''; | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
data+=chunk; | |
}); | |
res.on('end', function () { | |
if(typeof callback == 'function') { | |
callback(false,data); | |
} | |
}); | |
}); | |
req.on('error', function(e) { | |
if(typeof callback == 'function') { | |
callback(e,false); | |
} | |
}); | |
req.write(post_data); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment