javascript - 不完整的 Node HTTP.Response

标签 javascript json node.js rest httpresponse

我的应用程序构建了一个自定义的整数数组,需要将这些整数串联起来才能创建对经济数据的动态请求。

创建我的变量选项后,我通过一个 http 请求推送它并尝试解析响应,但我收到了一个错误 -

SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)

当我使用我的代码读取正文时,响应提前中断并且我没有在 console.log() 中获得完整响应。

我已经测试了请求值并将结果放在我的搜索栏中,并得到了肯定的结果。

关于为什么响应中的正文被缩短有什么想法吗?我需要从此请求中读取所有数据,并且在解析时出现错误。

这是代码-

var options = {
host: 'api.eve-central.com',
port: 80,
path: '/api/marketstat/json?typeid=18&typeid=19&typeid=20&typeid=21&typeid=22&typeid=34&typeid=35&typeid=36&typeid=37&typeid=38&typeid=39&typeid=40&typeid=41&typeid=42&typeid=43&typeid=44&typeid=45&typeid=49&typeid=50&typeid=51&typeid=52&typeid=164&typeid=165&typeid=166&typeid=178&typeid=179&typeid=180&typeid=181&typeid=182&typeid=183&typeid=184&typeid=185&typeid=186&typeid=187&typeid=188&typeid=189&typeid=190&typeid=191&typeid=192&typeid=193&typeid=194&typeid=195&typeid=196&typeid=197&typeid=198&typeid=199&typeid=200&typeid=201&typeid=202&typeid=203&typeid=204&typeid=205&typeid=206&typeid=207&typeid=208&typeid=209&typeid=210&typeid=211&typeid=212&typeid=213&typeid=215&typeid=216&typeid=217&typeid=218&typeid=219&typeid=220&typeid=221&typeid=222&typeid=223&typeid=224&typeid=225&typeid=226&typeid=227&typeid=228&typeid=229&typeid=230&typeid=231&typeid=232&typeid=233&typeid=234&typeid=235&typeid=236&typeid=237&typeid=238&typeid=239&typeid=240&typeid=241&typeid=242&typeid=243&typeid=244&typeid=245&typeid=246&typeid=247&typeid=248&typeid=249&typeid=250&typeid=251&typeid=252&typeid=253&typeid=254&usesystem=30000142',
method: 'GET'
}

function requester(options) {
http.request(options, function (res) {
    //console.log('STATUS: ' + res.statusCode);
    //console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
        var payload = JSON.parse(chunk);
        //console.log('After Parse: ' + payload);
        //Separating the payload into three pieces.
        buy = payload[0]["buy"];
        all = payload[0]["all"];
        sell = payload[0]["sell"];
    });
  }).end();
};

编辑:从代码中删除了会干扰测试的变量。

最佳答案

您需要在执行解析之前累积流式传输的数据,因为构成数据 block 的内容由 io 速率决定。

而是存储所有分块数据然后解析它

var options = {
  host: 'api.eve-central.com',
  port: 80,
  path: '/api/marketstat/json?typeid=18&typeid=19&typeid=20&typeid=21&typeid=22&typeid=34&typeid=35&typeid=36&typeid=37&typeid=38&typeid=39&typeid=40&typeid=41&typeid=42&typeid=43&typeid=44&typeid=45&typeid=49&typeid=50&typeid=51&typeid=52&typeid=164&typeid=165&typeid=166&typeid=178&typeid=179&typeid=180&typeid=181&typeid=182&typeid=183&typeid=184&typeid=185&typeid=186&typeid=187&typeid=188&typeid=189&typeid=190&typeid=191&typeid=192&typeid=193&typeid=194&typeid=195&typeid=196&typeid=197&typeid=198&typeid=199&typeid=200&typeid=201&typeid=202&typeid=203&typeid=204&typeid=205&typeid=206&typeid=207&typeid=208&typeid=209&typeid=210&typeid=211&typeid=212&typeid=213&typeid=215&typeid=216&typeid=217&typeid=218&typeid=219&typeid=220&typeid=221&typeid=222&typeid=223&typeid=224&typeid=225&typeid=226&typeid=227&typeid=228&typeid=229&typeid=230&typeid=231&typeid=232&typeid=233&typeid=234&typeid=235&typeid=236&typeid=237&typeid=238&typeid=239&typeid=240&typeid=241&typeid=242&typeid=243&typeid=244&typeid=245&typeid=246&typeid=247&typeid=248&typeid=249&typeid=250&typeid=251&typeid=252&typeid=253&typeid=254&usesystem=30000142',
  method: 'GET'
}

function requester(options) {
    http.request(options, function (res) {
        res.setEncoding('utf8');

        // Build response body in a string
        var resBody = '';

        // Listen for data and add
        res.on('data', function (chunk) {
            resBody += chunk
        });

        res.on('end', function () {   
            // Now that the response is done streaming, parse resBody           
            var payload = JSON.parse(resBody);

            //Separating the payload into three pieces.
            buy = payload[0]["buy"];
            all = payload[0]["all"];
            sell = payload[0]["sell"];

            itemStats.hiBuy = buy["max"];
            itemStats.loSel = sell["min"];
            itemStats.iskSpread = itemStats.loSel - itemStats.hiBuy; //Adjusted the spread calculation for station trading.
            itemStats.perSpread = itemStats.iskSpread / itemStats.loSel;
            itemStats.buyVol = buy["volume"];
            itemStats.selVol = sell["volume"];
            itemStats.allVol = all["volume"];
            itemStats.buyPerHR = itemStats.buyVol / 24;
            itemStats.selPerHR = itemStats.selVol / 24;

            console.log("Transferred Values" + JSON.stringify(itemStats));
        });
    });
};

关于javascript - 不完整的 Node HTTP.Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43174227/

相关文章:

javascript - YTPlayer 背景视频不自动播放

json - jq 不接受“.”line”作为有效语法 - "unexpected INVALID_CHARACTER"

perl - 如何从 JSON 数据中提取 URL?

html - 将值从 Express NodeJS 传递到 AngularJS Controller 变量

javascript - 为使用 dbpedia 的 sparql 查询生成 url

javascript - 解析 URL 参数并返回带有数组的 javascript 对象

node.js - ReactJS 从 Express 服务器下载文件

node.js - Node 集群与 Dynos 缩放

php - 有没有办法通过 PHP 自动将 XML 页面转换为 JSON?

javascript - 将 JSON 转换为数组