如何在node.js中处理带有身份验证的重定向响应
How to handle a redirect response with authentication in node.js?
学习目的我不使用外部模块,所以我试图做一个身份验证请求到服务器。它适用于curl:
curl -L -u user:password http://webpage/email
[解决了,谢谢…]但是在node.js中我有问题,这是我的代码:
var http = require("http");
var options = {
hostname : 'webpage',
port : '80',
path : '/email',
method : 'GET',
headers : {
"Connection" : "keep-alive",
"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64)"
},
auth : "username:password"
}
options.agent = new http.Agent(options);
var req = http.request(options, function(res) {
// The authentication works fine, like curl without -L parameter
// STATUS 302
res.setEncoding('utf8');
res.on('data',function(chunk){
console.log(chunk);
// SOLVED !
var opts = {
host : 'webpage',
port : '80',
path : '/email/',
location : res.headers.location,
auth : "user:password"
}
var require = http.request(opts,function(resp){
resp.setEnconding("utf8");
resp.on('data',function(chk){
console.log(chk);
});
});
require.end();
// --------------
// I got the same without -L parameter in curl
// <head><title>Document Moved</title></head>
// <body><h1>Object Moved</h1>This document may be found <a href="http://webpage/email/">here</a></body> <-- The 'Location' is the same
});
});
req.on('error',function(e){
console.log('Problem with request : ' + e.message);
}
req.end()
我试着在标题中再次请求"位置",但我得到了相同的结果。
谢谢你的帮助。
curl中的-u允许您传递Authentication头的值。在node中,您将手动执行此操作。基本身份验证(我假设您正在使用)的规范要求以base 64编码格式传递凭据。在node中手动执行此操作如下所示:
headers = {
'Authorization': 'Basic ' + (new Buffer(user + ':' + pass)).toString('base64')
}
相关文章:
- 当需要身份验证时,ui路由器不会重定向
- 全身份验证 - 抓住用户被重定向到谷歌的时刻
- 返回重定向到具有 HTTP 基本身份验证的 URL
- AngularFire,登录身份验证和重定向并不完全有效
- 护照身份验证回调而不是重定向
- Perl CGI 在身份验证后重定向
- 使用preventDefault时,PHP中的重定向身份验证表单
- 如果未经身份验证,请将用户重定向到登录页
- 使用node.js和angularjs将未经身份验证的用户重定向到默认URL
- 如果未通过身份验证,则在所有路由上重定向到登录
- 如何通过javascript中的表单验证重定向到PHP网页,并通过MySQL查询重定向到另一个页面
- 不重定向到身份验证日志的新访问令牌
- 雅虎身份验证通过OAuth没有任何重定向(客户端).这可能吗?
- 如何在node.js中处理带有身份验证的重定向响应
- 有什么方法可以绑定'任何'通过身份验证脚本重定向(链接/刷新等)
- 身份验证后使用Javascript SDK重定向URI
- Javascript -当单击按钮时,如果未经过身份验证,则重定向到Login页面
- NodeJS Passport身份验证-始终重定向到失败url
- 主干路由器:身份验证重定向
- Angularjs 登录身份验证重定向获取 -- 错误:发送标头后无法设置标头