当我使用给定 url 的 Node 请求库请求图像时,加载的图像不完整。存储加载的图像后,它看起来像 https://ibb.co/i5xVAR
但是请求完成时没有错误并且状态代码为 200。对我来说,ssl 连接似乎已关闭。浏览器或 curl 等其他工具将图像完整传输。
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']);
});
最佳答案
open.hpi.de 主机过早关闭连接。您可以向请求添加 Connection: keep-alive
header ,连接将保持打开状态,直到传输实际完成:
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
});
关于node.js - Node 获取请求响应不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47812126/