FCM旧版API调用以JSON_PARSING_ERROR响应:位置0的意外令牌END OF FILE

时间:2019-04-16 06:44:57

标签: node.js firebase firebase-cloud-messaging

我正在尝试对FCM服务器执行旧式调用,其中响应是意外的。那里显示了解析错误,到目前为止,对我而言这是无法跟踪的。我将在下面发布代码段。如果有任何丢失或错误编码的代码,请帮助我。

var request = require('request');

let message = {
"to": atoken,
"data": {
  title: "My first push notification",
  body: "Hello World!!!"
  }
  };

  // //let message = req.body.message;
  let key = "AIzaSyA1zIxVFJ3F7-_-XXXXXXXXXXXXXXXXXXX";
  request({
  method: 'POST',
  uri: 'https://fcm.googleapis.com/fcm/send',
  headers: {
  'Content-type': 'application/json',
  'Authorization': 'key=' + key
  },
  postData: JSON.stringify(message)
  },
  function (error, response, body) {
  if (error) {
    console.error('upload failed:', error);
    res.status(500).json({ error: true, data: { message: error.message } });
  } else {
    let data = response;
    console.log('Upload successful!  Server responded with:', 
    data.toJSON());
    //res.status(200).json(response);
   }
  });

即将到来的答复是:

 body:
 'JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.\n',
 headers:
 { 'content-type': 'text/plain; charset=UTF-8',
  date: 'Tue, 16 Apr 2019 06:34:05 GMT',
  expires: 'Tue, 16 Apr 2019 06:34:05 GMT',
  'cache-control': 'private, max-age=0',
  'x-content-type-options': 'nosniff',
  'x-frame-options': 'SAMEORIGIN',
  'x-xss-protection': '1; mode=block',
  server: 'GSE',
  'alt-svc': 'quic=":443"; ma=2592000; v="46,44,43,39"',
  'accept-ranges': 'none',
  vary: 'Accept-Encoding',
  connection: 'close' },
  request:
  { uri:
   Url {
   protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'fcm.googleapis.com',
    port: 443,
    hostname: 'fcm.googleapis.com',
    hash: null,
    search: null,
    query: null,
    pathname: '/fcm/send',
    path: '/fcm/send',
    href: 'https://fcm.googleapis.com/fcm/send' },
   method: 'POST',
   headers:
   { 'Content-type': 'application/json',
    Authorization: 'key=AIzaSyA1zIxVFJ3F7-_-hrIbTHTehNRmTUwG8Z0',
    'content-length': 0 } } }

1 个答案:

答案 0 :(得分:0)

尝试将request.post()中的postData: JSON.stringify(message)更改为body: JSON.stringify(message)