ESP8266 POST 请求到 Firebase Cloud HTTP 函数 returns 错误 500 Could not handle the request

ESP8266 POST request to Firebase Cloud HTTP Function returns Error 500 Could not handle the request

它在 Postman 上运行良好,但当我通过我的 ESP8266 尝试时出现错误。这是 ESP8266 的代码:

if(buttonState == HIGH) //send notification when button pressed
  {
    HTTPClient http;
    WiFiClientSecure client;
    client.setInsecure();
    //client.setFingerprint("A5:D4:06:4C:2A:4B:46:CD:C4:A5:51:7F:4C:F6:62:92:60:90:FD:37");
    http.begin(client, notifUrl);
    http.addHeader("Content-Type", "Content-Type: application/json"); 
    int httpResponseCode = http.POST(input);
    if(httpResponseCode>0)
    {
      String response = http.getString();  //Get the response to the request
      Serial.println(httpResponseCode);   //Print return code
      Serial.println(response);           //Print request answer
    } 
    else 
    {
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
      http.end();
    }
    delay(1000);
  }

这里是 Firebase 云函数:

exports.sendNotification = functions.https.onRequest((req, res) => {

  // Check for POST request
  if(req.method !== "POST"){
    res.status(400).send('Please send a POST request');
    return;
    }

    console.log(req.body);

    var message = {
        data: {
          title: 'Someone is at the door',
          body: 'Always check who it is before unlocking your door :)'
        },
        android: {
          priority: 'high',
        },
        token: req.body.token
      };

    // Send a message to the device corresponding to the provided
    // registration token.
    admin.messaging().send(message)
        .then((response) => {
            // Response is a message ID string.
            console.log('Successfully sent message:', response);
            res.status(200).send('Message sent successfully');
        })
        .catch((error) => {
            console.log('Error sending message:', error);
            res.status(500).send("Error sending message");
        });
});

注意云函数中的console.log(req.body)。我可以在尝试使用 Postman 时看到日志,但是当尝试使用 ESP 芯片时,该功能甚至没有到达 console.log 行,只是说功能已退出,状态为 'crash' 在日志中。

我正在绞尽脑汁想找出问题所在。感谢任何帮助。

这里的问题一定是因为您发送了错误的 HTTP Content-Type header。 您发送的是 Content-Type: Content-Type: application/json,而不是 Content-Type: application/json,我猜 Firebase 不太喜欢它!

在你的代码中应该是:

http.addHeader("Content-Type", "application/json");