javascript NodeJS - 如何通过请求下载文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18297003/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:22:43  来源:igfitidea点击:

NodeJS - How to Download file via Request?

javascriptjsonnode.jsdownloadrequest

提问by alongquan

I have a ExternalServe (running on Localhost) When I using Browser to request:

当我使用浏览器请求时,我有一个 ExternalServe(在本地主机上运行):

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

本地主机:2013/ExternalServer/getfilebyname?filename=getStatus.json

Then browser downloaded getStatus.json to Download Folder.

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

In my NodeJS project, I want to download getStatus.json file and I made:

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

download.js

下载.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();

but when i run: node download.js the system return

但是当我运行时: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>

How to fix it?

如何解决?

Best regard

最良好的问候

采纳答案by Ahsan Shah

You are getting the following error response:

您收到以下错误响应:

This request requires HTTP authentication

此请求需要 HTTP 身份验证

Suggesting to add the Authorization information in header. Like:

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

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);
});

in case of proxy, you can use the following header instead:

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

Proxy-Authorization

回答by Paul

The server wants a username and password, or requires another authorization mechanism, before it will let you access the file this way.

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

To see how to supply a user and password when making the request in node.js, look at How to use http.client in Node.js if there is basic authorization

要了解如何在 node.js 中发出请求时提供用户和密码,请查看如何在 Node.js 中使用 http.client 如果有基本授权

How do we know this could be the problem?

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

Two interesting strings in the system return:

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

HTTP Status 401

HTTP Status 401

and

This request requires HTTP authentication

This request requires HTTP authentication

From Wikipedia: List of HTTP Status Codes

来自维基百科:HTTP 状态代码列表

401 Unauthorized Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.[2] The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.

401 Unauthorized 与 403 Forbidden 类似,但专门用于需要身份验证但已失败或尚未提供的情况。 [2] 响应必须包含一个 WWW-Authenticate 头字段,其中包含适用于所请求资源的质询。请参阅基本访问身份验证和摘要访问身份验证。

Another possibility is that a server could be set up to emit 401 instead of 403, but doesn't really accept any usernames or passwords.

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