Mr.D
Mr.D

Reputation: 7893

Node JS does not correctly download file from github url

I want to download files from github. I use this question and it's answer as example

I write this code:

var downloadPath = './downloads/picasso.zip';
var file = fs.createWriteStream(downloadPath);

https.get('https://github.com/square/picasso/archive/master.zip', function (response) {
    response.pipe(file);

    file.on('finish', function() {
        file.close();
        console.log("DONE LOADING");
    })
}).on('error', function (err) {

    console.log("ERROR " + err.message);
    fs.unlink(downloadPath);
});

When I launch this code, it says to me DONE LOADING, which, in theory, means that zip file is downloaded. However, when I try to open this zip file, my archiver says that this file is corrupted and it is only 1KB.

Why this is happening? Does github restrict somehow downloading according to header of my request? Then why error is not throwing?

Upvotes: 2

Views: 1288

Answers (1)

xing
xing

Reputation: 64

wget file

You can see the HTTP status code 302, means it redirects to the other address codeload.github.com/square/picasso/zip/master

So you should replace the link in your code to the right address show above, then you will get the right file.

right file

Now the file is 1.2M

Upvotes: 4

Related Questions