var https = require('https');
var p = '/api/username/FA/AA?ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_KEY=dummy1234&ticket=dummy9876&ZOHO_API_VERSION=1.0';
var https = require('https');
var options = {
host: 'reportsapi.zoho.com',
port: 443,
path: p,
method: 'POST'
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
當我運行上面的代碼時,我得到下面的錯誤。如何在NodeJS中發送POST請求時設置Content-Length?
錯誤消息:
statusCode: 411
headers: { 'content-type': 'text/html',
'content-length': '357',
connection: 'close',
date: 'Thu, 24 Nov 2011 19:58:51 GMT',
server: 'ZGS',
'strict-transport-security': 'max-age=604800' }
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
411 - Length Required
如何修復的Abobe錯誤?
我曾嘗試做以下
var qs = 'ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_KEY=dummy1234&ticket=dummy9876&ZOHO_API_VERSION=1.0';
'
options.headers = {'Content-Length': qs.length}
但如果我嘗試這樣我得到以下錯誤:
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'socket hang up' }
任何人可以幫助我在這?
感謝
KOTI
PS:如果我進入了整個網址到瀏覽器地址欄,然後回車我得到JSON響應預期。
請不要使用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 –