NodeJS - How to download a file through a request?

I have ExternalServe (runs on Localhost) When I use a browser to query:

local: 2013 / ExternalServer / getfilebyname file_name = getStatus.json

Then the browser downloaded getStatus.json into the Download folder.

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

download.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 ran: node download.js the system returns

 <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?

Yours faithfully,

+6
source share
2 answers

You get the following error response:

This request requires HTTP authentication.

We suggest adding authorization information in the header. How:

 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 the case of a proxy server, you can use the following header instead:

 Proxy-Authorization 
+6
source

The server requires a username and password or requires a different authorization mechanism before it grants you access to the file in this way.

To learn how to specify a user and password when executing a request in node.js, see How to use http.client in node.js, if there is basic permission

How do we know that this can be a problem?

Two interesting lines in the system are returned:

HTTP Status 401

and

This request requires HTTP authentication

From Wikipedia: List of HTTP Status Codes

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

Another possibility is that the server can be configured to emit 401 instead of 403, but does not actually accept any usernames or passwords.

0
source

Source: https://habr.com/ru/post/951956/


All Articles