[英]socket hang up error in node.js post request
I'm trying to wrap a POST request to Parse using node.js. 我正在尝试使用node.js将POST请求包装为Parse。 I just want to send a Push notification, but a socket hang up error keeps showing up. 我只想发送一个Push通知,但是套接字挂起错误不断出现。
This is the REST API documentation for Parse Push: https://www.parse.com/docs/push_guide#top/REST 这是用于Parse Push的REST API文档: https : //www.parse.com/docs/push_guide#top/REST
It does work using curl and python. 它确实可以使用curl和python来工作。
This is the tutorial I checked to do the translation: http://tech.pro/tutorial/1091/posting-json-data-with-nodejs 这是我检查翻译的教程: http : //tech.pro/tutorial/1091/posting-json-data-with-nodejs
And this is my code: 这是我的代码:
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);
Thanks! 谢谢!
You are using the http module for doing an https call. 您正在使用http模块进行https调用。 You should require the https
at the top. 您应该在顶部要求https
。
Also be careful with pdata
as it's missing the var
keyword. 还请小心pdata
因为它缺少var
关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.