如果有基本授权,如何在Node.js中使用http.client

按照标题,我该怎么做? 这是我的代码:

var http = require('http'); var client = http.createClient(80, 'www.example.com'); // to access this url i need to put basic auth. var request = client.request('GET', '/', {'host': 'www.example.com'}); request.end(); request.on('response', function (response) { console.log('STATUS: ' + response.statusCode); console.log('HEADERS: ' + JSON.stringify(response.headers)); response.setEncoding('utf8'); response.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); 

您必须在标题中设置Authorization字段。

它包含身份validationtypes,在这种情况下的Basic和在Base64中被编码的username:password组合:

 var username = 'Test'; var password = '123'; var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); // auth is: 'Basic VGVzdDoxMjM=' var header = {'Host': 'www.example.com', 'Authorization': auth}; var request = client.request('GET', '/', header); 

从Node.js http.request API文档,你可以使用类似的东西

 var http = require('http'); var request = http.request({'hostname': 'www.example.com', 'auth': 'user:password' }, function (response) { console.log('STATUS: ' + response.statusCode); console.log('HEADERS: ' + JSON.stringify(response.headers)); response.setEncoding('utf8'); response.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); request.end(); 

更简单的解决scheme是直接在URL中使用用户:pass @ host格式。

使用请求库:

 var request = require('request'), username = "john", password = "1234", url = "http://" + username + ":" + password + "@www.example.com"; request( { url : url }, function (error, response, body) { // Do more stuff with 'body' here } ); 

我也写了一个关于这个的小博客 。

 var username = "Ali"; var password = "123"; var auth = "Basic " + new Buffer(username + ":" + password).toString("base64"); var request = require('request'); var url = "http://localhost:5647/contact/session/"; request.get( { url : url, headers : { "Authorization" : auth } }, function(error, response, body) { console.log('body : ', body); } ); 

什么是值得我在OSX上使用node.js 0.6.7,我不能得到'授权':validation与我们的代理工作,它需要设置为'代理授权':validation我的testing代码是:

 var http = require("http"); var auth = 'Basic ' + new Buffer("username:password").toString('base64'); var options = { host: 'proxyserver', port: 80, method:"GET", path: 'http://www.google.com', headers:{ "Proxy-Authorization": auth, Host: "www.google.com" } }; http.get(options, function(res) { console.log(res); res.pipe(process.stdout); }); 
 var http = require("http"); var url = "http://api.example.com/api/v1/?param1=1&param2=2"; var options = { host: "http://api.example.com", port: 80, method: "GET", path: url,//I don't know for some reason i have to use full url as a path auth: username + ':' + password }; http.get(options, function(rs) { var result = ""; rs.on('data', function(data) { result += data; }); rs.on('end', function() { console.log(result); }); }); 

我最近遇到这个。 在要设置的代理授权授权头中,哪一个取决于客户端正在与之通话的服务器。 如果它是一个Web服务器,你需要设置授权 ,如果它是一个代理,你必须设置代理授权标头

经过大量的研究,这个代码在我的情况下起作用。 您将需要安装请求npm包 。

 var url = "http://api.example.com/api/v1/?param1=1&param2=2"; var auth = "Basic " + new Buffer(username + ":" + password).toString("base64"); exports.checkApi = function (req, res) { // do the GET request request.get({ url: url, headers: { "Authorization": auth } }, function (error, response, body) { if(error) { console.error("Error while communication with api and ERROR is : " + error); res.send(error); } console.log('body : ', body); res.send(body); }); }