基于HTTP的NodeJS客户端:如何进行身份验证的请求? [英] HTTP Client based on NodeJS: How to authenticate a request?
本文介绍了基于HTTP的NodeJS客户端:如何进行身份验证的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是code要我做一个简单的GET请求:
This is the code I have to make a simple GET request:
var options = {
host: 'localhost',
port: 8000,
path: '/restricted'
};
request = http.get(options, function(res){
var body = "";
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
console.log(body);
})
res.on('error', function(e) {
console.log("Got error: " + e.message);
});
});
不过,这条道路/受限制需要一个简单的基本HTTP验证。如何添加凭据进行身份验证?我找不到相关的的NodeJS手动基本的HTTP认证什么。
先谢谢了。
But that path "/restricted" requires a simple basic HTTP authentication. How do I add the credentials to authenticate? I couldn't find anything related to basic http authentication in NodeJS' manual. Thanks in advance.
推荐答案
您需要将授权添加到象头部带的base64 codeD的选项。这样的:
You need to add the Authorization to the options like a header encoded with base64. Like:
var options = {
host: 'localhost',
port: 8000,
path: '/restricted',
headers: {
'Authorization': 'Basic ' + new Buffer(uname + ':' + pword).toString('base64')
}
};
这篇关于基于HTTP的NodeJS客户端:如何进行身份验证的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文