node.js – web-push未知问题,WebPushError“收到意外响应代码”400

在使用GCM和chrome开发
web-push期间.我最近遇到过这个问题(几小时前一切都还能正常工作)

我不确定是否由于GCM的某些变化或云的停机而发生这种情况……任何信息都将受到赞赏.

我的代码

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);
});

跟踪:

{
  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' 
}

最佳答案 @Lihn,粘贴你的有效载荷.从错误消息,似乎您发送格式错误的json消息.

您可以使用this service来检查json中的语法问题.

GCM docs声明状态代码400表示无法解析json消息.

点赞