arduino can't concat with char

I want to save POST data to a string variable, but i can’t do that ! i can display the POST on the Serial monitor, but can’t save it :frowning:

i think the problem here:

while(client.available())
                  {
                      char a = client.read(); // read 1 byte (character) from client
                      Serial.print(a);
                      if(!post_pass.concat(a))
                        Serial.print("\n error");  // save the HTTP request 1 char at a time
                      Serial.print(post_pass);
                  }

concat return failure and serial print error …

so post_pass always have just the first char :frowning:

Serial.print(a); show all the POST data, here is my full code in case someone need it:

#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 109); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

String HTTP_req;          // stores the HTTP request
String postPass;
int pin;
boolean auth = 0;


void setup()
{
    Serial.begin(9600);       // for diagnostics
    if(Ethernet.begin(mac) == 0)
        Serial.print("net not connected");  // initialize Ethernet device
    server.begin();           // start to listen for clients
    pinMode(2, OUTPUT);       // LED on pin 2
     Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                  while(client.available())
                  {
                      //Serial.write(client.read());
                      char a = client.read(); // read 1 byte (character) from client
                      postPass += a;  // save the HTTP request 1 char at a time
                      Serial.write(a);

                      //Serial.print(postPass);
                  }
                    Serial.println();
                    Serial.print(postPass);
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    // client.println("");
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<meta charset=\"utf-8\"><title>Arduino Smart Homes</title>");
                    client.println("<style>body { position:absolute; top:0; left:0; width:100%; margin:auto; right:0; min-height:100vh;font-size:10vh}</style>");
                    client.println("</head>");
                    client.println("<body>");
                    checkAuth(client);
                    if(auth == 1)
                    {
                      // the content of the HTTP response follows the header:
                      client.print("Click <a href=\"?on=8\">here</a> turn the LED on pin 8 on
");
                      client.print("Click <a href=\"?off=8\">here</a> turn the LED on pin 8 off
");
                      client.print("Click <a href=\"?on=9\">here</a> turn the LED on pin 9 on
");
                      client.print("Click <a href=\"?off=9\">here</a> turn the LED on pin 9 off
");
                      ProcessCheckbox(client);
                    }
                    else
                    {
                      client.print("<form method=\"post\">");
                      client.print("<label>Enter the password to continue</label>");
                      client.print("<input type=\"password\" name=\"pass\" value=\"\"/>");
                      client.print("<input type=\"submit\" name=\"submit\" value=\"submit\"/>");
                      client.print("</form>");

                    }                    
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
{
    if (HTTP_req.indexOf("on=") > -1) {  // see if checkbox was clicked
       pin = HTTP_req.substring(HTTP_req.indexOf("on=") + 3,HTTP_req.indexOf("on=") + 4).toInt();
       digitalWrite(pin, HIGH);
    }
     if (HTTP_req.indexOf("off=") > -1) {  // see if checkbox was clicked
       pin = HTTP_req.substring(HTTP_req.indexOf("off=") + 4,HTTP_req.indexOf("off=") + 4).toInt();
       Serial.print(pin);
       digitalWrite(pin, LOW);
     }
}
void checkAuth(EthernetClient cl)
{
 if (HTTP_req.indexOf("POST") > -1)
 {
    //postPass = HTTP_req.substring(HTTP_req.indexOf("POST") + 4,HTTP_req.indexOf("POST") + 9);
    //Serial.print(postPass);
    if (postPass == "pass1")
    {
      auth = 1;
    }
 }
}

You'd save some space if you used char arrays instead of the String class.

Be careful with the String class. Even if you get this to work you've got bugs hiding. String isn't very appropriate on a memory constrained system like a microcontroller and it's downright dangerous on a system with no garbage collection. It can fragment your RAM to the point that it crashes your program. And the concat function is the worst offender in its arsenal. You'd be better off using char arrays to hold the strings, not to mention the fact that it is a little easier to build a string one char at a time that way since you just need to drop them in the array and that's it.

in the statement

char a = client.read();

what happens if data has not arrived and read() returns -1 ?

@horace

He has it inside a test for client.available(). But what if only part of the message has arrived at that point.