尝试通过node.js http POST到“graph.facebook.com”时,发生“读取ECONNRESET”错误

尝试通过node.js http模块向“graph.facebook.com”发送POST请求,但每次都要读取“ECONNRESET”。

所有参数都是有效的 – 通过curl检查相同的请求,它的工作原理。

var http = require('http'); var token = "..."; sendTextMessage(XXX, "Text received"); function sendTextMessage(sender, text) { var messageData = { text:text }; var json = { recipient: {id:sender}, message: messageData, }; var body = JSON.stringify(json); var options = { host: "graph.facebook.com", path: '/v2.6/me/messages?access_token=' + token, port: 443, method: 'POST', headers: {'Content-Type': 'application/json', 'Content-Length': body.length } }; var callback = function(response) { console.log('Status: ' + response.statusCode); console.log('Headers: ' + JSON.stringify(response.headers)); var str = '' response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { console.log("end:" + str); }); } var req = http.request(options, callback); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); //This is the data we are posting, it needs to be a string or a buffer req.write(body); req.end(); } 

任何线索?

那么,终于find了答案

我必须更换

 var http = require('http') 

 var https = require('https')