json - 为什么我从这个 http 请求中得到空响应

标签 json node.js httprequest httpresponse

我正在编写我的第一个 Node.js 脚本,它只是向 https://www.swapi.co/api/people/?search=Luke+ 发出 http 请求并解析响应数据。

端点如下:

var options = {
  host: 'www.swapi.co',
  path: `/api/people/?search=`+firstName+'+'+lastName
};

逻辑是从响应中获取数据并将其解析为 person 对象:

makeRequest(options, function( data, error) {
    let person = data.results[0];
    if (person) {
        let height = person.height;
        let response = person.name + " is " + height + " centimeters tall.";
        callback(null, {"speech": response});
    }
    else {
        callback(null, {"speech": "I'm not sure!"});
    }
  });

makerequest函数的定义如下:

function makeRequest(options, callback) {
    var request = http.request(options, 
    function(response) {
        var responseString = '';
        response.on('data', function(data) {
            responseString += data;
        });
         response.on('end', function() {
            console.log('end: $$$' + responseString + '$$$');
            var responseJSON = JSON.parse(responseString);
            callback(responseJSON, null);
        });
    });
    request.end();
}

当我运行脚本时,我收到有关解析 JSON 的错误。

Unexpected end of JSON input
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/var/task/index.js:42:37)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

我使用 Postman 测试了端点并得到了以下 JSON 作为响应:

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "name": "Luke Skywalker",
            "height": "172",
            "mass": "77",
            "hair_color": "blond",
            "skin_color": "fair",
            "eye_color": "blue",
            "birth_year": "19BBY",
            "gender": "male",
            "homeworld": "https://www.swapi.co/api/planets/1/",
            "films": [
                "https://www.swapi.co/api/films/2/",
                "https://www.swapi.co/api/films/6/",
                "https://www.swapi.co/api/films/3/",
                "https://www.swapi.co/api/films/1/",
                "https://www.swapi.co/api/films/7/"
            ],
            "species": [
                "https://www.swapi.co/api/species/1/"
            ],
            "vehicles": [
                "https://www.swapi.co/api/vehicles/14/",
                "https://www.swapi.co/api/vehicles/30/"
            ],
            "starships": [
                "https://www.swapi.co/api/starships/12/",
                "https://www.swapi.co/api/starships/22/"
            ],
            "created": "2014-12-09T13:50:51.644000Z",
            "edited": "2014-12-20T21:17:56.891000Z",
            "url": "https://www.swapi.co/api/people/1/"
        }
    ]
}

但是,当我调试代码时,响应数据是空字符串。这解释了 JSON 错误。

我的http请求出了什么问题?为什么我没有得到正确的响应?

最佳答案

您所针对的 API 似乎仅支持 SSL,但 Node 的 HTTP库仅支持纯文本请求。尝试使用他们的 HTTPS改为图书馆。

var https = require('https');
var request = https.request(options, ...);

关于json - 为什么我从这个 http 请求中得到空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47274971/

相关文章:

json - Node.js 和 json.s

javascript - 无法将动态生成的可缓存链接数组传递给 Service Worker

node.js - sendPeersProposal - Promise 被拒绝 : Error: REQUEST_TIMEOUT, Hyperledger Fabric(余额传输示例)

node.js - 我可以强制 Nodejs 模块通过 TOR 发出请求而不直接修改该模块吗?

javascript - 什么是最好的、轻量级的 JSON/ajax 脚本?

php - 无效的 JSON : Invisible Chars?

java - 加载集成的 youtube channel 视频时出现 JSONException

javascript - 如何根据mongodb中的日期范围过滤数据

java - 如何在不打开浏览器或使用 Blackberry 中的 browserfield 的情况下连接到 URL

java - apache.org POST 请求错误