【问题标题】:401 File Download Attempt With Node-Wget401 文件下载尝试使用 Node-Wget
【发布时间】:2017-09-11 16:13:26
【问题描述】:

尝试使用 Node-Wget 下载文件:

wget ({
      url: 'https://localhost...',
      dest: './test_artifacts/logs/',
      timeout: 2000
      }, function (error, response, body) {
        if (error) {
            console.log('--- error:');
            console.log(error);            // error encountered 
        } else {
            console.log('--- headers:');
            console.log(response.headers); // response headers 
            console.log('--- body:');
            console.log(body);             // content of package 
        }
    }
);

另外,使用 PhantomJS、Selenium Webdriver 和 Cucumber。 上面的代码在文件公开可用时有效,但当我在我的网站上使用它时,我得到:

Status Code: 401; Unauthorized

该站点使用登录页面来验证用户。我认为这是阻止我下载文件的原因。不过,在测试步骤的这个阶段,Selenium 已经登录并浏览了该站点。这让我相信 Wget 需要传递一些东西来确定用户已经登录。问题是,node-wget 的文档非常小,我在文档中没有看到提供解决方案的命令。

正在使用的包的链接: https://www.npmjs.com/package/node-wget

【问题讨论】:

  • 您很可能需要添加一个授权标头,它看起来像是 node-wget 包支持的。见Authorization - HTTP
  • 感谢您的回复。在阅读了上面的链接后,我相信我理解了这个概念。但是,我在使用 node-wget 实现它时遇到了麻烦。尝试了以下标头的一些变体: Authorization: 'username:password',

标签: node.js selenium phantomjs cucumber wget


【解决方案1】:

切换到 npm 'request'。工作正常。允许自定义 http 标头字段,例如:

headers: { 
Authorization: 'Bearer esdfsdf...' 
}
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多