【问题标题】:Why do I get empty reponse from this http request为什么我从这个 http 请求中得到空响应
【发布时间】:2017-11-13 22:59:27
【问题描述】:

我正在编写我的第一个 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 请求有什么问题?为什么我没有得到正确的响应?

【问题讨论】:

    标签: json node.js httprequest httpresponse


    【解决方案1】:

    您的目标 API 似乎仅支持 SSL,但 Node 的 HTTP 库仅支持纯文本请求。尝试改用他们的 HTTPS 库。

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

    【讨论】:

      【解决方案2】:

      您使用的那个 URL 默认返回 HTML。

      您需要致电:https://www.swapi.co/api/people/?format=json&search=Luke+

      (注意format=json参数)

      【讨论】:

      • 我用path: '/api/people/?format=json&amp;search='+firstName+'+'+lastName编辑了options,结果还是一样
      • 无论有无format 参数,我都得到了正确的JSON 响应。听起来您的请求实际上可能得​​到了错误的响应。我注意到的一件事是,您似乎没有将 HTTPS 协议附加到请求中,这可能会导致空响应,尽管我认为 Node 会很好地遵循重定向。
      • 刚刚自己尝试过,并且能够重现您所看到的问题。显然 Node 的 HTTP 库不支持 SSL。看我的回答。
      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      相关资源
      最近更新 更多