Search code examples
node.jscompressionhttp-postpostdatamsgpack

How to receive post data in node.js as sent?


Client sending POST request with msgpack data as postbody. I can receive the chunk data as string, msgpack module not able to accept the string input for unpack the data. how to receive the post data as msgpack (not as string)? so that msgpack.unpack can be performed to get the actual data.


Solution

  • Just concatenate the request data. It could be as easy as:

    var bufs = [], buflen = 0;
    
    req.on('data', function(chunk) {
      bufs.push(chunk);
      buflen += chunk.length;
    }).on('end', function() {
      var data = Buffer.concat(bufs, buflen);
      bufs = undefined; // let GC clean up references sooner
      // use data here ...
    });