Node.js的POST请求没有完成

问题描述:

在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',...),看看是否给你提供线索。 –

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

。你应该只写'logs' : [{'test' : 'value1'}]。外部JSON.stringify将负责将整个JSON object转换为string