【问题标题】:NodeJS - How to Download file via Request?NodeJS - 如何通过请求下载文件?
【发布时间】:2013-08-20 06:12:03
【问题描述】:

我有一个 ExternalServe(在 Localhost 上运行) 当我使用浏览器请求时:

localhost:2013/ExternalServer/getfilebyname?filename=getStatus.json

然后浏览器将getStatus.json下载到下载文件夹。

在我的 NodeJS 项目中,我想下载 getStatus.json 文件,我做了:

下载.js

var http = require('http');
var fs = require('fs');

function getFile (){

  var file = fs.createWriteStream("./../lib/user.json");
  var req = http.get("http://localhost:2013/ExternalServer/getfilebyname?filename=getStatus.json", function(res) {
    res.pipe(file);
});
}

getFile();

但是当我运行时:node download.js 系统返回

<html><head><title>Apache Tomcat/8.0.0-RC1 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>This request requires HTTP authentication.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/8.0.0-RC1</h3></body></html>

如何解决?

最好的尊重

【问题讨论】:

    标签: javascript json node.js download request


    【解决方案1】:

    您收到以下错误响应:

    此请求需要 HTTP 身份验证

    建议在header中添加授权信息。喜欢:

    var options = {
        host: 'localhost',
        port: 2013,
        path: '/ExternalServer/getfilebyname?filename=getStatus.json',
        headers: {
         'Authorization': 'Basic ' + new Buffer(uname + ':' + pword).toString('base64')
       }         
    };
    
    request = http.get(options, function(res) {
       res.pipe(file);
    });
    

    在代理的情况下,您可以使用以下标头:

    Proxy-Authorization
    

    【讨论】:

    • 如果我的账号和密码都是“tomcat”,那么 'Authorization': 'Basic' + new Buffer("tomcat" + ':' + "tomcat).toString('base64') ?
    • 非常感谢你,成功了... :D
    • 成功,但是:保存后,系统返回一个文件:[object Object]在根文件夹....我无法修复它..
    【解决方案2】:

    服务器需要用户名和密码,或者需要其他授权机制,才能让您以这种方式访问​​文件。

    要了解如何在 node.js 中发出请求时提供用户名和密码,请查看 How to use http.client in Node.js if there is basic authorization

    我们怎么知道这可能是问题所在?

    系统返回两个有趣的字符串:

    HTTP Status 401

    This request requires HTTP authentication

    来自Wikipedia: List of HTTP Status Codes

    401 Unauthorized 与 403 Forbidden 类似,但专门用于 当需要身份验证并且已失败或尚未验证时 提供。[2]响应必须包含 WWW-Authenticate 标头字段 包含适用于所请求资源的质询。见基本 访问认证和摘要访问认证。

    另一种可能性是服务器可以设置为发出 401 而不是 403,但实际上并不接受任何用户名或密码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 2011-04-19
      • 2015-10-08
      • 2010-11-05
      相关资源
      最近更新 更多