Skip to content

Instantly share code, notes, and snippets.

@whyleee
Created August 9, 2015 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whyleee/dbb4018e20d418ecda6c to your computer and use it in GitHub Desktop.
Save whyleee/dbb4018e20d418ecda6c to your computer and use it in GitHub Desktop.
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