Search code examples
node.jshttpsriot-games-api

Incomplete JSON response with Node.js https-module


Calling the Riot-Api Im receiving incomplete JSON on a https GET-request.

After debugging, I realized that depending how much I wait (breakpoint) pre-executing the https on'data' callback Im actually receiving the complete JSON object.

(Average API response time for me is 200-300ms)

let getOptions = function(url) {
  return {
    host:  'na.api.pvp.net',
    port: 443,
    path: `${url}?api_key=${apiKey}`,
    method: 'GET'
  };
}

exports.Call = function(url, callback) {
  let response = {};

  let req = https.request(getOptions(url), function(res) {
    response.statusCode = res.statusCode;
    res.on('data', function(data) {
      response.json = JSON.parse(data);
      callback(response);
    });
  });

  req.on('error', function(err) {
    response.err = err;
    callback(response);
  }); 

  req.end();
};

Running the code without breakpoints or only breaking a short time I run either into error:

JSON.parse(data): Unexpected Token in JSON at position ...

or

JSON.parse(data): Unexptected end of JSON Input.

As I expect the 'data' callback to be executed only after the request is complete im confused about how to fix it (without artificially delaying it ofc.).


Solution

  • http.request returns a stream – its not a simple callback that contains the whole response. You will have to buffer and concatenate everything if you want to parse the whole response.

    I would strongly recomment to use a helper library like got or request