0

My request is a multipart/form-data

This is how I read the request data:

var data = "";

this.request.on( "data" , function( chunk ){

    data += chunk;

} );

this.request.on( "end" , function(){

    this.request.body = this.parseRequest( data );

    this.emit( "end" );

}.bind( this ) );

Now, the Content-Length of the request is 25,981, but the length of data "on end" is 25,142.

Can anyone explain please?

Michael Sazonov
  • 1,531
  • 11
  • 21

1 Answers1

0

The problem was here:

data += chunk;

The .toString() method in buffer corrupts the binary data - that's where the length loss comes from.

Two solutions:

  1. Keep chunk as a Buffer and work with it.
  2. Use .toString('binary') which converts the buffer to string with no data loss (at least in my test).

My code now:

    var buffer = [];

    this.request.on( "data" , function( chunk ){
        buffer.push( chunk );
    } );

    this.request.on( "end" , function(){

        buffer = Buffer.concat( buffer );
        this.request.body = this.parseRequest( buffer.toString("binary") );
        this.emit( "end" );

    }.bind( this ) );
Michael Sazonov
  • 1,531
  • 11
  • 21