2012-10-26 263 views
0

如何根據內容設置請求的內容長度?根據節點內容設置請求的內容長度

例如:

POST /Display HTTP/1.0 
Content-Type: application/json 
Content-Length: 125 

{"QueryReq": 
    { 
    "Tid": "Tid-123456", 
    "SessionId" : "1350711351232058820" 
    } 
} 

上登載在遠程登錄這一請求,我需要按輸入多次,直到內容長度爲125是空的組塊是按照服務器接收。如何根據請求內容自動設置內容長度?

+0

你在你的節點服務器做POST請求? – vinayr

+0

是的。做post請求 – vinod

回答

1

Content-Length

var data = querystring.stringify({ 
    "QueryReq": { "Tid": "Tid-123456", "SessionId" : "1350711351232058820" } 
}); 

var options = { 
    host: xxx, 
    port: xxx, 
    ---- 
    ---- 
    headers: { 
     'Content-Length': data.length 
    } 
}; 
+4

請不要使用data.length,我碰到這個問題,作者說不要使用data.length,而是使用Buffer.byteLength(data)。參考問題:http://stackoverflow.com/questions/18692580/node-js-post-causes-error-socket-hang-up-code-econnreset和ref問題:https://github.com/visionmedia/express/問題/ 1749 –

+0

@NamNguyen是正確的,我只是花了這麼多年[緩衝區問題](http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers)。此外,你甚至不需要設置長度[發送'Content-length'標題將禁用默認分塊編碼](http://nodejs.org/api/http.html#http_http_request_options_callback) –