2016-05-15 141 views
0

在node.js中我使用的HTTP請求模塊,使該請求Node.js的POST請求沒有完成

var post_req = http.request({ 
    host: 'api.site1.ciscozeus.io', 
    path: '/logs/' + ZUES_TOKEN + '/' + logName + '/', 
    port: 80, 
    method: 'POST', 
    headers: { 
     'Content-type' : 'application/x-www-form-urlencoded' 
    } 
}, function(res) { 
    res.on('data', function (chunk) { 
     console.log(1); 
     cb(chunk); 
    }); 
}); 
post_req.write(JSON.stringify({ 
    "logs" : JSON.stringify('[{"test":"value1"}]') 
})); 
post_req.on('error', function(e) { 
    console.error(e); 
}); 
post_req.end(); 

但我的問題是,它似乎並沒有進入cb(chunk);。就像它不叫最終諾言功能一樣。它不打印console.log(1)。

該api有一個測試網站,試試看,如果我嘗試它,它的工作原理。下面是當我檢查它,當它工作在該工具如何在日誌數據看:

enter image description here enter image description here

有誰知道,如果我不當附加在日誌數據?我不想發佈相同的數據。

由於

+0

不需要在已經是字符串的東西上調用JSON.stringify()。您的請求可能不正確。聽res.on('err',...),看看是否給你提供線索。 –

回答

0
post_req.write(JSON.stringify({ 
    "logs" : JSON.stringify('[{"test":"value1"}]') 
})); 
在代碼的內部 JSON.stringify是不正確的上述部分

。你應該只寫'logs' : [{'test' : 'value1'}]。外部JSON.stringify將負責將整個JSON object轉換爲string