0

Im using the following code to download a zip file. If you put the URL into a browser you will see that it starts downloading. But when I use Node to download the zip, the output test.zip file contains this:

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://patentscdn.reedtech.com/ipg150804-zip/ipg150804.zip?sv=2014-02-14&amp;sr=b&amp;sig=u1W37AFnH9DAcqc7k4tDABaV14eSlchN8fJJ1%2FOkWEo%3D&amp;st=2015-08-04T04%3A00%3A00Z&amp;se=9999-12-31T05%3A00%3A00Z&amp;sp=r">here</a></body>

Heres the Node.js code:

var http = require('http');
var fs = require('fs');
var url = 'http://patents.reedtech.com/downloads/GrantRedBookText/2015/ipg150804.zip';

http.get(url, function(response) {
    response.on('data', function(data) {
        fs.appendFileSync('test.zip', data);
    });
    response.on('end', function() {
        console.log('hello');
    });
});
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109

1 Answers1

0

There is an HTTP redirect, you can do the following

var http = require('follow-redirects').http;

this will enable http to follow redirects. I suggest you use request library.

Check How do you follow an HTTP Redirect in Node.js? for more details

Community
  • 1
  • 1
AhmadAssaf
  • 3,556
  • 5
  • 31
  • 42
  • Thanks this works, only note was that I had to npm install follow-redirects. Also I tried it using request like you suggested and it worked as well. Heres the code `request(url).pipe(fs.createWriteStream('./test.zip'))` – Daniel Kobe Aug 07 '15 at 14:18