节点获取请求响应不完整

时间:2017-12-14 11:22:17

标签: node.js ssl httprequest

当我使用给定网址的节点请求库请求图片时,加载的图片不完整。存储加载的图像后,它看起来像https://ibb.co/i5xVAR

但是请求完成没有错误并且状态代码为200.对我来说,似乎ssl连接已关闭。浏览器或卷曲等其他工具可以完整地传输图像。

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

1 个答案:

答案 0 :(得分:1)

open.hpi.de主机提前关闭连接。您可以在请求中添加Connection: keep-alive标头,连接将保持打开状态,直到转移完成为止:

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