Created
August 9, 2015 19:14
-
-
Save whyleee/dbb4018e20d418ecda6c to your computer and use it in GitHub Desktop.
js-git node.js request implementation
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 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