【问题标题】:socket hang up error in node.js post requestnode.js 发布请求中的套接字挂起错误
【发布时间】:2014-03-21 01:53:00
【问题描述】:

我正在尝试使用 node.js 将 POST 请求包装到 Parse。我只想发送推送通知,但不断出现套接字挂起错误。

这是我的代码:

var http = require('http')

var data = {
    channels: 'a_channel',
    data: {
            alert: "Something has happened"
    }
 }

 pdata = JSON.stringify(data);

 var apiKeys =  {
   "X-Parse-Application-Id": "lol",
   "X-Parse-REST-API-Key": "lolol",
   "Content-Type": "application/json",
    'Content-Length': pdata.length
};

 var options = {
    hostname: 'api.parse.com',
    port: 443,
    path: '/1/push',
    method: 'POST',
    headers: apiKeys
};


var req = http.request(options, function(res) {
res.setEncoding('utf-8');

var responseString = '';
 res.on('data', function(data) {
 responseString += data;
  });

 res.on('end', function() {
var resultObject = JSON.parse(responseString);
   });
   });

req.on('error', function(e) {
   console.log('problem with the request, wero: ' + e.message);
});


   req.write(pdata);
   req.end();
   console.log(pdata);

谢谢!

【问题讨论】:

    标签: node.js parsing post https push


    【解决方案1】:

    您正在使用 http 模块进行 https 调用。您应该要求顶部的https

    还要小心 pdata,因为它缺少 var 关键字。

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 2021-01-02
      • 1970-01-01
      相关资源
      最近更新 更多