B4R Question ESP32 - http.POST not passing on values to PHP

rabbitBUSH

Active Member
Licensed User
I've been banging at this one for a while, read a lot of forums, tutorials, searched this forum and all and all.

Pretty much the active parts of the code below worked for someone else, so, I can't figure out now why this isn't working.

As below : this hooks to my site with a PHP script that should be getting the POSTed data, then, stuffing it into the Mysql data table.

When the PHP pops there is no data, so the POST array has not been populated.

In the code : I have put in comment lines (about mid way) showing which line is causing a 400 html page error : BAD REQUEST

Incidentally, I have an alternative sketch to do this and that returns a HTML code 200 (success) BUT there is no data in the POST array either. It also uses http.POST which is where the 200 code is returned to >>int httpResponseCode = http.POST(httpRequestData);<<
In both cases then the http.POST fails.

The code below is my preferred method

Hook web site post data to Mysql:
void POSTxneelo() {

  const char serverHost [] = "www.xxxxxxx.nom.za";

  WiFiClient client;
  const int httpPort = 80;

  //Send an HTTP POST request

    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
        HTTPClient http;

      if(!client.connect(serverHost, httpPort)) {
           Serial.println ("Connection Failed");
            return;
            }
      else {
              Serial.println ("</br>>>> Connection SUCCEEDED....</br></br>");
              }

// debug line  checking to see if the data values are there
        Serial.println ((String) "HTTP/1.1\r\nGET http://www.xxxxxxx.nom.za/php/weather_data/esp-post-data.php?" +
                        "&datadate=''" +
                        "&BMP_temp=" + temperature +
                        "&BMP_pressure=" + pressure +
                        "&DHT_temp=" + dhttemperature +
                        "&DHT_rh=" + dhthumidity +
                        "&DHT_absrh=" + dhtabsolute +
                        "&WIND_speed=" + 0 +
                        "&WIND_direction=" + 0 +
                        "&heat_index=" + dhtheatindex +
                        "&dew_point=" + dhtdewpoint +
                        "&comf_ration=" + dhtcomfort +
                        "&comf_percept=" + dhtperception +
                        "&DATA_record=''" +
                        "Host: " + serverHost + "\r\n" +
                        "Connection: close\r\n\r\n");
[COLOR=rgb(41, 105, 176)]/*
*   // This will send the request to the server
*  this little bit is just dropped off somewhere on some site to test if the connection works
*      THIS LINE WORKS WHEN UNCOMMENTED AND THE NEXT ONE IS COMMENTED OUT
* client.print((String)"GET / HTTP/1.1\r\n" + "Host: " + String(serverHost) + "\r\n" + "Connection: close\r\n\r\n");
*/[/COLOR]

/* /// this sends the data to mysql* */

[COLOR=rgb(41, 105, 176)]//  AT RUN TIME THE REMOTE APACHE SERVER GIVES A [B]400 ERROR : BAD REQUEST[/B]
// OTHERWISE THE SKETCH RUNS AND DROPS THROUGH TO THE LINES BELOW AND DISPLAYS THE RETURN FROM APACHE
// EVERYTHING ELSE WORKS EVEN THE php WHICH WRITES ZEROS TO THE DATABASE AT THE SAME TIME AS THE esp32 READS DATA AND
// TRIES TO SEND AND ADD TO THE DATABASE.

// SO THIS IS THE OFFENDING LINE
// THE STRING LOOKS CORRECT WHEN SENT TO THE SERIAL MONITOR
// IT FAILS WHETHER ITS A get OR A post[/COLOR]
        client.print (String ("GET http://www.xxxxxxx.nom.za/php/weather_data/esp-post-data.php?") +
                        ("&datadate=") + "" +        <<-- that is dummy its a timestamp insertion by MYSQL
                        ("&BMP_temp=") + temperature +
                        ("&BMP_pressure=") + pressure +
                        ("&DHT_temp=") + dhttemperature +
                        ("&DHT_rh=") + dhthumidity +
                        ("&DHT_absrh=") + dhtabsolute +
                        ("&WIND_speed=") + 0 +
                        ("&WIND_direction=") + 0 +
                        ("&heat_index=") + dhtheatindex +
                        ("&dew_point=") + dhtdewpoint +
                        ("&comf_ration=") + dhtcomfort +
                        ("&comf_percept=") + dhtperception +
                        ("&DATA_record=") + "" +       <<-- that also dummy its PRIMARY AUTO INCREMENT
                        "HTTP/1.1\r\n" +
                        "Host: " + serverHost + "\r\n" +
                        "Connection: close\r\n\r\n");

/**/
        unsigned long timeout = millis();
   
        while (client.available() == 0) {
              if (millis () - timeout > 10000) {
                  Serial.println ("</br></br>>>> Client Timeout !</br>");
                  client.stop();
                  return;
                  }
                  }
       
        // read all the lines of the reply from the server  get a return string as HTML displayed in serial monitor
        while (client.available ()) {
                String line = client.readStringUntil('\r');
                Serial.print (line);
                }
   
  Serial.println ();
  Serial.println ("CLOSING CONNECTION\r\n\r\n");
  client.stop();
      }
}
 
Top