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

根据标题,我该怎么做?

Here is my code:

var http = require('http');


// to access this url I need to put basic auth.
var client = http.createClient(80, 'www.example.com');


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);
});
});
189942 次浏览

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

它包含本例中的身份验证类型 Basic和在 Base64中编码的 username:password组合:

var username = 'Test';
var password = '123';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
// new Buffer() is deprecated from v6


// auth is: 'Basic VGVzdDoxMjM='


var header = {'Host': 'www.example.com', 'Authorization': auth};
var request = client.request('GET', '/', header);

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

使用 request库:

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
}
);

关于这个我也写了一些 博客文章

不管怎样,我在 OSX 上使用 node.js 0.6.7,但是我无法获得‘ Authorization’: auth 来使用我们的代理,它需要被设置为‘ Proxy-Authorization’: auth 我的测试代码是:

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);
});

我最近偶然发现了这个。 要设置的 代理-授权授权头中的哪个取决于客户机正在与之交谈的服务器。 如果是 Webserver,则需要设置 授权,如果是代理,则必须设置 代理-授权

来自 < a href = “ http://nodejs.org/API/http.html # http _ http _ request _ options _ callback”> Node.js http.request API Docs 你可以用类似于

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();
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);
});
});
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);
} );

经过大量的研究,这段代码在我的案例中起作用。您需要安装 请求 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);


});
}

对于那些不使用 DNS 并且需要更多深度的用户(你也可以使用 request代替 get,只需用 request代替 get,如下: http.request({ ... })) :

http.get({
host: '127.0.0.1',
port: 443,
path: '/books?author=spongebob',
auth: 'user:p@ssword#'
}, resp => {
let data;


resp.on('data', chunk => {
data += chunk;
});


resp.on('end', () => console.log(data));
}).on('error', err => console.log(err));