-->
Page 1 of 1

Get HTTP status code in WiFiClientSecure

PostPosted: Thu Aug 20, 2020 10:45 pm
by Fede M.
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);
}

Re: Get HTTP status code in WiFiClientSecure

PostPosted: Fri Aug 21, 2020 2:15 pm
by JurajA
parse the first line of the HTTP response

Re: Get HTTP status code in WiFiClientSecure

PostPosted: Mon Aug 24, 2020 12:20 am
by sammythebull
if you get an 401 error, you get a response from the server. SSL HTTP connection worked, only authorization_code has wrong value.