![](/img/trans.png)
[英]In node.js, how do I get the Content-Length header in response to http.get()?
[英]Node.JS request data length is smaller than given in Content-Length
我的要求是多部分/表格數據
這就是我讀取請求數據的方式:
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 ) );
現在,請求的Content-Length
為25,981 ,但“結束”數據的長度為25,142 。
誰能解釋一下?
問題在這里:
data += chunk;
緩沖區中的.toString()
方法損壞了二進制數據-這就是長度損失的來源。
兩種解決方案:
.toString('binary')
,它將緩沖區轉換為沒有數據丟失的字符串(至少在我的測試中如此)。 我的代碼現在:
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 ) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.