Unexpected end of input - curl JSON on Express / Node App

I have an Express / Node application that is deployed on a Heroku instance. The application has a POST api point that expects a .json file, reads data, and populates the application with JSON data. Below is the code that processes 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 execute the query using 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 loads data, and at other times it generates an "Unexpected input end" error. It seems that the buffer method used does not read all the data for some reason. When registering data.length in the above code (like JSON) it seems that the request is interrupted whenever the data length is less than expected. Is there something wrong with the way I read in the JSON file above? Thanks -

+5
source share
2 answers

The data event only indicates that some data was received - not all data was received. You want to wait for the end event before parsing JSON. For more information, see this example from the official documentation: https://nodejs.org/api/stream.html#stream_api_for_stream_consumers

+4
source

Content-Length should be the number of octets of the response body.

if you try to use the value

 data.length 

but not

 Buffer.byteLength(data) 
+1
source

Source: https://habr.com/ru/post/1243349/


All Articles