Rubycon
Rubycon

Reputation: 18346

Getting "read ECONNRESET" error when trying to POST to 'graph.facebook.com' via node.js http

Trying to do a POST request to 'graph.facebook.com' via node.js http module, but getting "read ECONNRESET" every time.

All parameters are valid - checked the same request via curl and it works.

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();
}

any clue?

Upvotes: 1

Views: 6101

Answers (1)

Rubycon
Rubycon

Reputation: 18346

Well, finally found the answer

I have to replace

var http = require('http')

with

var https = require('https')

Upvotes: 2

Related Questions