3

Hi I'm having problems to perform HTTP request on NodeJS given a larger number array of json object. The request works fine given small array of json object. However, if I try to increase the size array of json, I received Error: socket hang up {"error":{"code":"ECONNRESET"}}. Is it required to perform multiple write? Or is it something wrong going on at the other end?

Thanks in advance for taking your time here!

    // data is a json object

    var post_data = JSON.stringify(data);
    var buf = new Buffer(post_data);
    var len = buf.length;

    var options = {
        hostname: address,
        port: port,
        path: pathName,
        method: 'PUT',
        headers: {
            'Content-Type':'application/json',
            'Content-Length': len,
            'Transfer-Encoding':'chunked'
        }
    };

    // http call to REST API server
    var req = restHttp.request(options, function(res) {

        console.log('server PUT response received.');
        var resData = '';
        res.on('data', function(replyData) {

            // Check reply data for error.
            console.log(replyData.toString('utf8'));
            if(replyData !== 'undefined')
                resData += replyData;
        });

        res.on('end', function() {          
            callback(JSON.parse(resData));
        });
    }); 


    req.write(buf);
    req.end();
Hong Zhou
  • 659
  • 1
  • 9
  • 20
  • You can stream the request body. Read [this](http://www.sitepoint.com/introduction-to-streams/) post for the details. – Bulkan Nov 05 '13 at 12:45
  • Thanks for the reply. So this is an issue related to the streaming? Could u provide an example how it could apply in this situation? – Hong Zhou Nov 05 '13 at 13:04
  • if the data in `buf` was in a [readable stream](http://nodejs.org/api/stream.html#stream_class_stream_readable) then you can just do `buf.pipe(req)`. For example, if the current directory contains a file `data.json` with the JSON you can do `var buf = fs.createReadStream(__dirname + '/data.json');` to create a `ReadStream` object. Then you can pipe this to you req. – Bulkan Nov 05 '13 at 13:18
  • Update: buf is a Buffer object. Could you provide the example as an answer so that I could give you credit for this help :) Thanks! – Hong Zhou Nov 05 '13 at 13:31
  • Awesome. I added the answer below :) – Bulkan Nov 05 '13 at 13:34
  • Did you ever get it to work? I have similar issue though I am not sending data from a buffer, but rather as application/x-www-form-urlencoded so it is all in encoded string... – Daniel Gruszczyk Jul 01 '15 at 14:52
  • Hi Daniel, which version of nodejs are u using? – Hong Zhou Jul 02 '15 at 11:04

1 Answers1

2

You can stream the request body.

If the data in buf was in a readable stream then you can just do buf.pipe(req).

For example, if the current directory contains a file data.json with the JSON you can do

var buf = fs.createReadStream(__dirname + '/data.json');

to create a ReadStream object. Then you can pipe this to you req

buf.pipe(req);

The pipe command will call req.end once its done streaming.

Bulkan
  • 2,555
  • 1
  • 20
  • 31