The use of the ESP8266 in the world of IoT

User avatar
By Fede M.
#88406 I have a function that sends a post request to a server using WiFiClientSecure and I need to get the http status code when data is returned but I can't figure it out.

Here is my function

host and fingerprint are defined globally

Code: Select allvoid sendUsage(String url) {
  WiFiClientSecure client;
 
  Serial.print("connecting to: ");
  Serial.println(host);

  //Serial.printf("Using fingerprint '%s'\n", fingerprint);
  client.setFingerprint(fingerprint);

  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
 
  Serial.print("Accessing URL: ");
  Serial.print(host);
  Serial.println(url);

  String body = "data=sampleData";
 
 
   client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ESP8266" + "\r\n" +
               "Content-Type: application/x-www-form-urlencoded\r\n" +
               "Content-Length: " + body.length() + "\r\n" +
               "Connection: close\r\n" + "\r\n"+ body);

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received: ");
      Serial.println(line);
      break;
    }
  }
 
  if (check if status is 200) {
    Serial.println("Server received data successfully");
  } else {
    Serial.print("Error sending data to server: Status is ");
    Serial.println(client.status());
  }

  Serial.println("closing connection");
  delay(200);
}