sir_thursday
sir_thursday

Reputation: 5409

Unexpected End of Input - curl JSON on Express/Node App

I have an Express/Node app that is deployed to a Heroku instance. The app has a POST api endpoint that expects a .json file, reads the data, and populates the app with the JSON data. Below is the backend code that handles the POST request:

router.route('/data')
  .post(function (req, res) {
    return DataUtils.storeData(req, res);
  });

Utils.storeData = function (req, res) {
  req.pipe(req.busboy);
  req.busboy.on('file', function (fieldname, file, filename) {
    file.on('data', function(data) {
      var data;
      try {
        data = JSON.parse('' + data);  // quick casting into a String
        var numPersonSessionObj = data.length;
        ...
        // etc etc...

When we try to make the request via the curl command below:

curl -H "Content-Length: 5567" -F file=@sample_data/sample_json.json http://[heroku-instance]/api/data

The server sometimes works fine, and uploads the data, and other times, throws an "Unexpected end of input" error. It seems as though the buffer method we're using is not reading all of the data for some reason. Upon logging the data.length in the code above (i.e. the JSON), it seems as though the request fails whenever the data length is less than its supposed to be. Is there something wrong with how I'm reading in the JSON file above? Thanks--

Upvotes: 5

Views: 1482

Answers (2)

user1419950
user1419950

Reputation: 326

Content-Length must be the count of octets the response body.

if you try using the value of

data.length

rather than

Buffer.byteLength(data)

Upvotes: 1

user5964259
user5964259

Reputation:

The data event only indicates that some data was received – not that all data was received. You want to wait for the end event before parsing your JSON. See this example from the official documentation for further details: https://nodejs.org/api/stream.html#stream_api_for_stream_consumers

Upvotes: 4

Related Questions