Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Client Error #228

Closed
rumkin opened this issue Apr 21, 2016 · 9 comments
Closed

HTTP Client Error #228

rumkin opened this issue Apr 21, 2016 · 9 comments

Comments

@rumkin
Copy link

rumkin commented Apr 21, 2016

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
Copy link
Collaborator

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
Copy link
Author

rumkin commented Apr 21, 2016

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
Copy link
Collaborator

@hypesystem any ideas?

@hypesystem
Copy link
Collaborator

No idea, no...

@hypesystem
Copy link
Collaborator

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
Copy link
Collaborator

eladnava commented Apr 26, 2016

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
Copy link
Author

rumkin commented Apr 28, 2016

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
Copy link
Collaborator

eladnava commented Apr 28, 2016

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

Line 109:

var sender_options = this.options || {};

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

@rumkin
Copy link
Author

rumkin commented May 5, 2016

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

Thanks for help.

@rumkin rumkin closed this as completed May 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants