var http = require('q-io/http'), url = require('url'), _ = require('underscore'); module.exports = function(urlOrObject, options) { options = options ? _.clone(options) : {}; options.url = _.isString(urlOrObject) ? url.parse(urlOrObject) : _.clone(urlOrObject); // wrap content into q-io thingy if ( options.body ) { options.body = { forEach: function(callback) { callback(JSON.stringify(options.body)); } }; }; // add auth header since q-io doesn't support this if ( options.url.auth ) { options.headers.authorization = 'Basic '+(new Buffer(options.url.auth).toString("base64")); } // make request and collect results return http.request(options) .then(function(res){ return res.body.read().then(function(content){ return content.length > 0 ? JSON.parse(content) : null; }); },function(res){ return res.body.read().then(function(error){ return error.length > 0 ? JSON.parse(error) : null; }); }); };