2016-04-19 81 views
1

嘗試通過node.js http模塊向'graph.facebook.com'發出POST請求,但獲取「每次閱讀ECONNRESET「。嘗試通過node.js POST到'graph.facebook.com'時發生「讀ECONNRESET」錯誤http

所有參數都是有效的 - 通過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(); 
} 

任何線索?

回答

1

好了,終於找到了答案

我不得不更換

var http = require('http') 

var https = require('https') 
相關問題