【发布时间】:2014-03-21 01:53:00
【问题描述】:
我正在尝试使用 node.js 将 POST 请求包装到 Parse。我只想发送推送通知,但不断出现套接字挂起错误。
这是解析推送的 REST API 文档:https://www.parse.com/docs/push_guide#top/REST
它确实可以使用 curl 和 python。这是我检查的翻译教程:http://tech.pro/tutorial/1091/posting-json-data-with-nodejs
这是我的代码:
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