Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web-push unknown issue, WebPushError "Received unexpected response code" 400

During the development of web-push with GCM and chrome. I've encountered this issue recently (couple hours ago everything still working fine)

I am not sure if this is happen due to some changes at GCM or a downtime of the cloud... Any information will be appreciated.

My code

var payload = JSON.stringify({
    title: 'notification title',
    body: 'message body'
});
webPush.sendNotification(params.endpoint, {
    userPublicKey: params.userPublicKey,
    userAuth: params.userAuth,
    payload: payload
}).then(function (res) { 
    // console.log of stuff
})
.catch(function(error){
    console.log(error);
    process.exit(1);
});

Trace:

{
  WebPushError: Received unexpected response code
  at IncomingMessage.<anonymous> (/var/www/***/pwa/node_modules/web-push /index.js:264:20)
  at emitNone (events.js:91:20)
  at IncomingMessage.emit (events.js:185:7)
  at endReadableNT (_stream_readable.js:926:12)
  at _combinedTickCallback (internal/process/next_tick.js:74:11)
  at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'WebPushError',
  message: 'Received unexpected response code',
  statusCode: 400,
  headers: {
    'content-type': 'text/plain; charset=UTF-8',
     date: 'Wed, 04 May 2016 11:35:13 GMT',
     expires: 'Wed, 04 May 2016 11:35:13 GMT',
    'cache-control': 'private, max-age=0',
    'x-content-type-options': 'nosniff',
    'x-frame-options': 'SAMEORIGIN',
    'x-xss-protection': '1; mode=block',
     server: 'GSE',
    'alternate-protocol': '443:quic',
    'alt-svc': 'quic=":443"; ma=2592000; v="33,32,31,30,29,28,27,26,25"',
    'accept-ranges': 'none',
     vary: 'Accept-Encoding',
     connection: 'close'
  },
  body: 'JSON_PARSING_ERROR: Unexpected token END OF FILE at position 467.\n' 
}
like image 770
Linh Pham Avatar asked Oct 18 '22 10:10

Linh Pham


2 Answers

@Lihn, paste here your payload. From error the message, it seems like you are sending a malformed json message.

You can use this service to check for syntax problems in your json.

The GCM docs states that a status code 400 means the json message could not be parsed.

like image 112
joarleymoraes Avatar answered Oct 21 '22 11:10

joarleymoraes


If you are trying to send push notifications using web-push, try regenerating the public and private key using #npm run web-push generate-vapid-keys. Try again by replacing these keys. Good Luck.

like image 36
Yogesh Bhanushali Avatar answered Oct 21 '22 09:10

Yogesh Bhanushali