I want to send a text with umlauts (äüöß) with GCM, but I always get the following exception when sending the request:
{"to":"/topics/mytopic","data":{"message":"ö;ä;1471125600000;1471211999999"}}
undefined:1
JSON_PARSING_ERROR: Unexpected token END OF FILE at position 106.
^
SyntaxError: Unexpected token J
at Object.parse (native)
at IncomingMessage.<anonymous> (c:\project\path\gcm.js:43:30)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
When I send a text without umlauts, everxthing works fine and I get a result.
Here my JavaScript function:
exports.sendMessage = function (req, res) {
var topic = req.body.topic;
var message = req.body.message;
var data = JSON.stringify({
"to": "/topics/" + topic,
"data": {
"message": message
}
});
var options = {
host: 'gcm-http.googleapis.com',
path: '/gcm/send',
method: 'POST',
headers: {
'Authorization': 'key=xxx',
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': data.length
}
};
var req2 = http.request(options, function(resp) {
var msg = '';
resp.setEncoding('utf-8');
resp.on('data', function(chunk) {
msg += chunk;
});
resp.on('end', function() {
console.log(JSON.parse(msg));
if(JSON.parse(msg).message_id) {
res.send(200, {msg: "Message sent successfully! Message: " + JSON.parse(msg).message_id});
} else {
res.send(400, {msg: "Message sending failed! Message: " + JSON.stringify(msg)});
}
});
});
req2.write(data);
req2.end();
};
What am I doing wrong? Do I need to re-encode the message
string before sending?
I finally got it after hours of searching - removing the Content-Length
did the trick for me. So my request options
now look the following:
var options = {
host: 'gcm-http.googleapis.com',
path: '/gcm/send',
method: 'POST',
headers: {
'Authorization': 'key=xxx',
'Content-Type': 'application/json; charset=utf-8'
// NO CONTENT-LENGTH PARAMETER ANYMORE!!!
}
};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments