After adding basic authentication client.readStringUntil('\r') is always empty

Hello everyone,

I am trying to add basic authentication to my project and my issue is that after doing this the client.readStringUntil(’\r’); is always empty.

This was my previous code:

void loop()
{

WiFiClient client = server.available();
  String  request = "";
  boolean DataAvailable = false;
  if (client) 
{
    // Read requests from web page if available
    //----------------------------------------------------------------------
    request = client.readStringUntil('\r');
    DataAvailable = true;
    client.flush();
  }
}

This is what i have tried to do with basic authentication:

 void loop()
{

WiFiClient client = server.available();

if (client) { // If a new client connects,
  Serial.println("New Client."); // print a message out in the serial port
  String currentLine = ""; // make a String to hold incoming data from the client
  while (client.connected()) { // loop while the client's connected
    if (client.available()) { // if there's bytes to read from the client,
      char c = client.read(); // read a byte, then
      header += c;
      if (c == '\n') { // if the byte is a newline character
        // if the current line is blank, you got two newline characters in a row.
        // that's the end of the client HTTP request, so send a response:
        if (currentLine.length() == 0) {
          // checking if header is valid
          // dXNlcjpwYXNz = 'user:pass' (user:pass) base64 encode
          if (header.indexOf("dXNlcjpwYXNz") >= 0) {
          // Finding the right credential string, then loads web page
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
// ******************************            
// i have tried adding here the lines from my
// previous code but the result of client.readStringUntil('\r'); is always empty 
            request = client.readStringUntil('\r');
            DataAvailable = true;
            client.flush();
// ******************************            
          }
          // Wrong user or password, so HTTP request fails... 
          else {
            client.println("HTTP/1.1 401 Unauthorized");
            client.println("WWW-Authenticate: Basic realm=\"Secure\"");
            client.println("Content-Type: text/html");
            client.println();
            client.println("<html>Authentication failed</html>");
            break;
          }
        } else { // if you got a newline, then clear currentLine
          currentLine = "";
        }
      } else if (c != '\r') { // if you got anything else but a carriage return character,
        currentLine += c; // add it to the end of the currentLine
      }
    }
  }
  // Clear the header variable
  header = "";
  // Close the connection
  client.stop();
  Serial.println("Client disconnected.");
}
}
}

Can someone please help me understand why request = client.readStringUntil(’\r’); is returning an empty value after i added the authentication?

Thank you very much!

Any help is highly appreciated :slight_smile:
Thank you!

print exactly what's coming

void loop()
{

  WiFiClient client = server.available();

  if (client) { // If a new client connects,
    Serial.println("New Client."); // print a message out in the serial port
    String currentLine = ""; // make a String to hold incoming data from the client
    while (client.connected()) { // loop while the client's connected
      if (client.available()) { // if there's bytes to read from the client,
        char c = client.read(); // read a byte, then
        switch (c) {
          case '\r': Serial.print("\r"); break;
          case '\n': Serial.println("\n"); break;
          default: Serial.print(c); break;
        }
      }
    }
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
  }
}

then you parse accordingly

Hello J-M-L,
I am adding the code as new code besides the authentication one or should I combine them?
Thank you!

This is to print what you exactly get. There is no code handling the authentication there.
Once you know what’s really coming you can parse it correctly.

(Alternatively you coud print what you get in your got and instead of doing a readStringUntil() just check if there is anything left to read and print that char by char