1

I use nodeMCU to connect to FCM API to send a push notification to a mobile application. I tested out in postman everything works fine and send a push notification to mobile but the problem when I use httpclient in Arduino it gives -1 error if I did https const char* cloudMessagingServer = "https://fcm.googleapis.com/fcm/send"; when I use http only without SSL const char* cloudMessagingServer = "http://fcm.googleapis.com/fcm/send"; gives a 403 error code

My code

  const char* cloudMessagingServer = "fcm.googleapis.com/fcm/send";

  char json[] = "{\"to\":\"<my_mobile_token>\",\"notification\":{\"title\":\"Hello\",\"body\":\"Yellow\"}}";


  HTTPClient http;
  http.begin(cloudMessagingServer);
  http.addHeader("Authorization", "key=<my_key>");
  http.addHeader("Content-Type", "application/json"); 
  int httpResponseCode = http.POST(json);
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

After some search I found that should add fingerprint of host to create a SSL request

My code after adding fingerprint

  const char* cloudMessagingServer = "fcm.googleapis.com/fcm/send";

  char json[] = "{\"to\":\"<my_mobile_token>\",\"notification\":{\"title\":\"Hello\",\"body\":\"Yellow\"}}";
  const char* fingerprint =            "a9 07 09 14 98 12 22 b8 58 3f db 6e 93 36 f7 3f 13 e2 72 82";

  HTTPClient http;
  http.begin(cloudMessagingServer, fingerprint);
  http.addHeader("Authorization", "key=<my_key>");
  http.addHeader("Content-Type", "application/json"); 
  int httpResponseCode = http.POST(json);
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

when I use https in all cases give me -1

1
  • 1
    https:// is missing in the url. the right way is to create a WiFiClentSecure object and provide it to HttpClient. see the BasicHttpsClient example of the ESP8266HttpClient library – Juraj May 9 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.