Last change
on this file since 466 was
466,
checked in by hendrikvanantwerpen, 12 years ago
|
Added authentication (fixed user now).
|
File size:
1.1 KB
|
Rev | Line | |
---|
[466] | 1 | var http = require('q-io/http'), |
---|
| 2 | url = require('url'), |
---|
| 3 | _ = require('underscore'); |
---|
| 4 | |
---|
| 5 | module.exports = function(urlOrObject, options) { |
---|
| 6 | |
---|
| 7 | options = options |
---|
| 8 | ? _.clone(options) |
---|
| 9 | : {}; |
---|
| 10 | options.url = _.isString(urlOrObject) |
---|
| 11 | ? url.parse(urlOrObject) |
---|
| 12 | : _.clone(urlOrObject); |
---|
| 13 | |
---|
| 14 | // wrap content into q-io thingy |
---|
| 15 | if ( options.body ) { |
---|
| 16 | options.body = { |
---|
| 17 | forEach: function(callback) { |
---|
| 18 | callback(JSON.stringify(options.body)); |
---|
| 19 | } |
---|
| 20 | }; |
---|
| 21 | }; |
---|
| 22 | |
---|
| 23 | // add auth header since q-io doesn't support this |
---|
| 24 | if ( options.url.auth ) { |
---|
| 25 | options.headers.authorization = 'Basic '+(new Buffer(options.url.auth).toString("base64")); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | // make request and collect results |
---|
| 29 | return http.request(options) |
---|
| 30 | .then(function(res){ |
---|
| 31 | return res.body.read().then(function(content){ |
---|
| 32 | return content.length > 0 ? JSON.parse(content) : null; |
---|
| 33 | }); |
---|
| 34 | },function(res){ |
---|
| 35 | return res.body.read().then(function(error){ |
---|
| 36 | return error.length > 0 ? JSON.parse(error) : null; |
---|
| 37 | }); |
---|
| 38 | }); |
---|
| 39 | }; |
---|
Note: See
TracBrowser
for help on using the repository browser.