Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
js-git node.js request implementation
var https = require('https');
var urlParse = require('url').parse;
function request(method, url, headers, body, callback) {
if (typeof body == 'function') {
callback = body;
body = undefined;
}
var parsedUrl = urlParse(url);
var options = {
method: method,
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.path,
headers: headers
};
var req = https.request(options, onResponse);
if (body) {
req.write(body);
}
req.on('error', function(e) {
callback(e);
});
req.end();
function onResponse(res) {
var buf = new Buffer(0);
res.on('data', function(chunk) {
buf = Buffer.concat([buf, chunk]);
});
res.on('end', function() {
callback(null, {
statusCode: res.statusCode,
headers: res.headers,
body: buf
});
});
}
}
module.exports = request;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment