ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 206 forks source link

HTTP Client Error #228

Closed rumkin closed 8 years ago

rumkin commented 8 years ago

I've got http error on sending gcm pushes:

_http_outgoing.js:348
    throw new TypeError('The header content contains invalid characters');
    ^

TypeError: The header content contains invalid characters
    at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:348:11)
    at new ClientRequest (_http_client.js:85:14)
    at Object.exports.request (http.js:31:10)
    at Object.exports.request (https.js:196:15)
    at Request.start (/app/source/node_modules/request/request.js:803:30)
    at Request.write (/app/source/node_modules/request/request.js:1376:10)
    at end (/app/source/node_modules/request/request.js:560:16)
    at Immediate._onImmediate (/app/source/node_modules/request/request.js:588:7)
    at tryOnImmediate (timers.js:534:15)
    at processImmediate [as _immediateCallback] (timers.js:514:5)
eladnava commented 8 years ago

Hey @rumkin, Can you please post the code you are using to send the notification?

My first guess would be that you passed an invalid API key in the following line:

// Set up the sender with you API key
var sender = new gcm.Sender('YOUR_API_KEY_HERE');
rumkin commented 8 years ago

My API key was working during this morning before nodejs version update from 5.4 to 5.10.

I'm sending pushes with this code:

this.gcm = new gcm.Sender('YOUR_API_KEY_HERE'); // ...with proper API key

// ....
var message = new gcm.Message();

message.addData({
    text: text,
    sound: sound,
    badge: badge,
    params: JSON.stringify(data)
});

this.gcm.send(message, {registrationIds: [token]}, function (error) {
    if (error) {
        console.error(error);
    }
});
eladnava commented 8 years ago

@hypesystem any ideas?

hypesystem commented 8 years ago

No idea, no...

hypesystem commented 8 years ago

From a similar issue:

Solved: OS language wasn't english, so chars in header were not identified -> TypeError('The header content contains invalid characters')

Could this be the problem?

According to this issue from request, which we use underlying, it can happen when URLs contain invalid characters. The fix is calling encodeURI(decodeURI(url)).

The error is thrown, and not propagated properly. It seems that this can be fixed by updating the request version we use.

eladnava commented 8 years ago

Interesting theory!

Would love to verify it -- @rumkin, can you add the following code to node_modules/node-gcm/lib/sender.js and then try sending a push notification? It will echo the request headers to the Node.js console.

Find in sender.js:

var post_req = req(request_options, function (err, res, resBodyJSON) {

Add below:

console.log(res.req._header);

Then, try to send a push notification and paste the headers here, please.

rumkin commented 8 years ago

Nope.

/app/source/node_modules/node-gcm/lib/sender.js:109
        console.log('gcm_headers', res.req._header);
                                      ^

TypeError: Cannot read property 'req' of undefined

But there is a error object which content is equal to the first comment's error.

eladnava commented 8 years ago

Looks like you're using an outdated version of node-gcm.

Line 109: https://github.com/ToothlessGear/node-gcm/blob/c00e43fabbbb1d3cd824350fed23a122efe4f99a/lib/sender.js#L109

Which version are you using now? Try to update node-gcm to 0.14.0.

rumkin commented 8 years ago

Solved. Problem was in '\n' char in the key. Probably key should be validated?

Thanks for help.