parse = require('url').parse,
queryString = require('querystring')
/**
* Request using the given _method_, _url_
* followed by optional _headers_, and _data_.
* Mega super awesome private request utility.
*
* @param {string} method
* @param {string} url
* @param {hash} headers
* @param {hash} data
* @param {hash} headers
* @param {function} fn
* @param {number} redirects
* @api private
*/
function request(method, url, headers, data, fn, redirects) {
function request(method, url, data, headers, fn, redirects) {
var buf = '',
redirects = redirects || 3,
url = parse(url),
path = url.pathname || '/',
data = queryString.stringify(data)
headers['content-length'] = data.length
headers['content-type'] = 'application/x-www-form-urlencoded'
}
require('sys').p('req')
var req = client.request(method, path + search + hash, headers)
if (data) req.write(data)
req.addListener('response', function(res){
if (req.statusCode < 200 || req.statusCode >= 400)
* @api private
*/
function client(method) {
return function(url, headers, data, fn) {
return request(method.toUpperCase(), url, headers, data, fn)
return function() {
var headers, data,
args = Array.prototype.slice.call(arguments),
url = args.shift(),
fn = args.pop(),
data = args.shift(),
headers = args.shift()
if (typeof fn !== 'function')
throw new TypeError('http client requires a callback function')
return request(method.toUpperCase(), url, data, headers, fn)
}
}
// --- Public API
exports.post = exports.create = client('post')
exports.put = exports.update = client('put')
exports.del = exports.destroy = client('delete')
exports.get('http://google.com', {}, {}, function(e, content){
exports.get('http://google.com', function(e, content){
if (e) throw e
require('sys').p(content.length)
})