Intermittent 400 Bad Request/408 Timeout Error

We are getting an intermittent (anywhere from 1/20 to 1/5) 400 Bad Request/408 Timeout errors.
Communication is between a Spark Core and Uniform Server XI running on a Windows x64 machine.

Core sends a PUT request via the following code:

    /*----------Server Varables----------*/  
    	byte server[] = {000, 000, 000, 000};
        String line1("PUT /get_test.php HTTP/1.1\r\n");
    	String line2("User-Agent: A Spark Core\r\n");
    	String line3("Host:\r\n");
    	String line4("Connection: close\r\n");
    	String line5("Content-Type: text/plain\r\n");
    	String line6("Content-Length: ");
//line 7 is generated
    	String line8("\r\n");
    	String line9("\r\n");
    	String line10("\r\n");  


    TCPClient client;
    if(client.connect(server, 80)) {
        client.write( (uint8_t*)line1.c_str(), line1.length() );
        client.write( (uint8_t*)line2.c_str(), line2.length() );
        client.write( (uint8_t*)line3.c_str(), line3.length() );
        client.write( (uint8_t*)line4.c_str(), line4.length() );
        client.write( (uint8_t*)line5.c_str(), line5.length() );
        client.write( (uint8_t*)line6.c_str(), line6.length() );
        client.write( (uint8_t*)line7.c_str(), line7.length() );
        client.write( (uint8_t*)line8.c_str(), line8.length() );
        client.write( (uint8_t*)line9.c_str(), line9.length() );
        client.write( (uint8_t*)message.c_str(), message.length() );
        client.write( (uint8_t*)line10.c_str(), line10.length() );
        //Serial.println("PUT request sent");
        //Serial.print("Data Sent");
        int i = 0;

          if(i == 0) {
          Serial.println("entered loop ");
          i = 1;
          char c =;

The Apache logs look (more or less) like this:

[12/Aug/2014:16:09:18 -0400] "PUT /get_test.php HTTP/1.1" 200 - "-" "A Spark Core"
[12/Aug/2014:16:09:28 -0400] "PUT /get_test.php HTTP/1.1" 400 226 "-" "A Spark Core"
[12/Aug/2014:16:23:33 -0400] "PUT /get_test.php HTTP/1.1" 408 221 "-" "A Spark Core"

The error is neither regular nor reliably triggered. Delays between transmits have ranged from 30 seconds to 5 seconds with very little difference in result. Delays after client.stop() have had no effect.
Another side effect is that the server return is not read by the while loop, although the server does send back an html response.

Any insight or feedback is appreciated.

Hi sigma,

I had the same problem sending data to my RaspPi. After investigation the TCP communication, I found that multiple client.write commands are send as several ip packets. Then I used one string variable to hold all data for the request. Since then the errors are gone. Also importend: you must send a blank line at the end of the data.

This is code I use successfully on my weather station

void sendTestData(void){
    if(client.connect(server, 80)){
        string HTTPreq = "GET /debugdata.php";
        sprintf(szData, "%.2f", dhtd.t); HTTPreq = HTTPreq + "?temp=" + szData;
        sprintf(szData, "%.2f", dhtd.h); HTTPreq = HTTPreq + "&humidity=" + szData;

        sprintf(szData, "%d", tcsd.colortemp); HTTPreq = HTTPreq + "&colortemp=" + szData;
        sprintf(szData, "%d", tcsd.lux); HTTPreq = HTTPreq + "&brightness=" + szData;
        sprintf(szData, "%d", tcsd.r); HTTPreq = HTTPreq + "&red=" + szData;
        sprintf(szData, "%d", tcsd.g); HTTPreq = HTTPreq + "&green=" + szData;
        sprintf(szData, "%d", tcsd.b); HTTPreq = HTTPreq + "&blue=" + szData;
        sprintf(szData, "%d", tcsd.c); HTTPreq = HTTPreq + "&clear=" + szData;
        sprintf(szData, "%d", bmpd.pressure / 100); HTTPreq = HTTPreq + "&pressure=" + szData;
        sprintf(szData, "%.2f", ina219d.voltage); HTTPreq = HTTPreq + "&voltage=" + szData;
        sprintf(szData, "%.2f", ina219d.current); HTTPreq = HTTPreq + "&current=" + szData;
        sprintf(szData, "%d", rssi); HTTPreq = HTTPreq + "&rssi=" + szData;
        HTTPreq = HTTPreq + "\n";
        HTTPreq = HTTPreq + "Host:\n";
        HTTPreq = HTTPreq + "User-Agent:\nContent-Length: 0\nConnection: close\n"; // add a new line to the end
        client.println(HTTPreq); // send data with extra new line
        delay(100); // wait first some time for response
        while (client.available())
            char c =;

Hope it helps

Thank you for your input. Unfortunately we still experienced the error, and have implemented a Spark-side buffer as a workaround. Thanks for pointing out the missing final newline, though.