user3498131
user3498131

Reputation: 27

node get request response incomplete

When I request images using node request library of the given url, the images loaded are not complete. After storing the loaded image it looks like https://ibb.co/i5xVAR

However the request finishes without an error and has status code 200. For me it seems that the ssl connection gets closed. Other tools like browser or curl transfer the image complete.

const request = require('request');
    const r1 = request({
        url: 'https://open.hpi.de/files/f1d16619-9813-4d59-96b3-d84908929b23',
        encoding: 'binary'
      }, (err, response, body) => {
        if (err) {
         console.log(err);
         return;
        }
        // complete file should be loaded
        // content and body length should match
        // read ECONNRESET should not be thrown
        console.log('body length', body.length);
        console.log('response content length', response.headers['content-length']);
      });

Upvotes: 1

Views: 886

Answers (1)

Will
Will

Reputation: 2191

The open.hpi.de host is closing the connection prematurely. You can add a Connection: keep-alive header to the request and the connection will remain open until the transfer is actually complete:

const request = require('request');
const r1 = request({
  url: 'https://open.hpi.de/files/f1d16619-9813-4d59-96b3-d84908929b23',
  encoding: 'binary',
  headers: {
    "Connection": "keep-alive"
  }
}, (err, response, body) => {
  // do the things
});

Upvotes: 2

Related Questions