javascript https 请求基本认证 node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26534969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
https request basic authentication node.js
提问by mammuthone
I am really getting crazy looking for this over the web and stackoverflow. Other posts about this topic talk of http request, not httpS.
我真的很疯狂地通过网络和 stackoverflow 寻找这个。关于此主题的其他帖子谈论的是 http 请求,而不是 httpS。
I'm coding server side with node.js and I need to make an https request to another website to login
我正在使用 node.js 编码服务器端,我需要向另一个网站发出 https 请求才能登录
If I use postman tool in chrome trying with https://user:[email protected]/esse3/auth/Logon.doeverything works fine and I log in.
如果我在 chrome 中使用邮递员工具尝试https://user:[email protected]/esse3/auth/Logon.do一切正常,我登录。
If I use request library in node I can't login and I get a page with a custom error message about an error in my getting/sending data.
如果我在节点中使用请求库,我将无法登录,并且会收到一个页面,其中包含有关获取/发送数据错误的自定义错误消息。
Maybe I am wrong setting the options to pass to request.
也许我错误地设置了传递给请求的选项。
var request = require('request');
var cheerio = require('cheerio');
var user = 'xxx';
var pass = 'yyy';
var options = {
url : 'https://webstudenti.unica.it',
path : '/esse3/auth/Logon.do',
method : 'GET',
port: 443,
authorization : {
username: user,
password: pass
}
}
request( options, function(err, res, html){
if(err){
console.log(err)
return
}
console.log(html)
var $ = cheerio.load(html)
var c = $('head title').text();
console.log(c);
})
回答by Alex Hill
http/https should make no difference in the authentication. Most likely your user/pass needs to be base64 encoded. Try
http/https 应该对身份验证没有影响。很可能您的用户/通行证需要进行 base64 编码。尝试
var user = new Buffer('xxx').toString('base64');
var pass = new Buffer('yyy').toString('base64');
请参阅:https: //security.stackexchange.com/questions/29916/why-does-http-basic-authentication-encode-the-username-and-password-with-base64
回答by mscdex
You're not setting your http auth optionscorrectly (namely authorization
should instead be auth
). It should look like:
您没有正确设置http 身份验证选项(即authorization
应该是auth
)。它应该看起来像:
var options = {
url: 'https://webstudenti.unica.it',
path: '/esse3/auth/Logon.do',
method: 'GET',
port: 443,
auth: {
user: user,
pass: pass
}
}
回答by Adiii
With the updated version, I am able to make https call with basic auth.
使用更新版本,我可以使用基本身份验证进行 https 调用。
var request = require('request');
request.get('https://localhost:15672/api/vhosts', {
'auth': {
'user': 'guest',
'pass': 'guest',
'sendImmediately': false
}
},function(error, response, body){
if(error){
console.log(error)
console.log("failed to get vhosts");
res.status(500).send('health check failed');
}
else{
res.status(200).send('rabbit mq is running');
}
})