ESP32 WIFI webclient returns 400 Bad Request

Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

ESP32 WIFI webclient returns 400 Bad Request

Postby Pcborges » Wed Aug 15, 2018 11:09 pm

Hi, have been trying to make this web request work for a while but no success.

This same request on a browser works good (copied from the serial terminal as the ESP32 generates it):
caxiasmed.com/esp32/insert.php?jd=1234567&area=Area01&type=h&value=125
Assistance welcome.
Code below.
Thanks
Paulo

Code: Select all

#include <WiFi.h>

const char* ssid     = "SkyNet";
const char* password = "ba21ca";
const char* host = "caxiasmed.com";

int    jd    = 1234567;
char  *area  = "Area01";
char   type  = 'h';
int    value = 125;

void setup(){
    Serial.begin(115200);

    // We start by connecting to a WiFi network
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void loop(){
    String  url;

    Serial.print("connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
    } 
    url  = "/esp32/insert.php?";
    url += "jd=";            
    url += jd;
    url += "&area=";            
    url += area;
    url += "&type=";
    url += 'h';
    url += "&value=";
    url += value;

    Serial.print("Requesting URL: ");
    Serial.println(url);


    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

/*
    Serial.println("#################################URL:");
    Serial.print("GET ");
    Serial.print(url);
    Serial.println(" HTTP/1.1");
    Serial.print("Host: ");
    Serial.println(host);
    Serial.println("Connection: close");
    Serial.println();
    Serial.println("#####################################");
*/                 
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 5000) {
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }

    // Read all the lines of the reply from server and print them to Serial
    while(client.available()) {
        String line = client.readStringUntil('\r');
        Serial.print(line);
    }

    Serial.println();
    Serial.println("closing connection");

    delay(15000);
}

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: ESP32 WIFI webclient returns 400 Bad Request

Postby kolban » Thu Aug 16, 2018 3:31 am

Howdy Paulo,
If I may suggest ... can you describe the nature of the error? While posting that "It doesn't work" and posting the source code is ok, it won't be as fruitful to getting you an answer as providing as much description as possible as to what actually happens when you do run the app. For example, does something crash? Is there a hang? Is there bad data sent or received?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

Re: ESP32 WIFI webclient returns 400 Bad Request

Postby Pcborges » Thu Aug 16, 2018 10:39 am

Hi, tanks for your response.

I do not have the prototype connected right now but what I have as error is the serial console message.
When I run the sketch with the serial console on I get the confirmation of the WiFi connection.
In the serial console I also get confirmation that the http connection with the "host" took place successfully then, when this following section of the script is executed:

Code: Select all

    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
Something goes wrong and the web server returns a html formatted error message that is printed on the serial console when this line is executed:

Code: Select all

    // Read all the lines of the reply from server and print them to Serial
    while(client.available()) {
        String line = client.readStringUntil('\r');
        Serial.print(line);
    }
This response includes the message 400 BAD REQUEST as if the GET above was incorrectly formatted.

For your reference I am including the insert.php that is actually called by the ESP32 code posted previously:

Code: Select all

<?php
   
   include('connection.php');

   $jd    = $_GET['jd'];
   $area  = $_GET['area'];
   $type  = $_GET['type'];
   $value = $_GET['value'];


   $sql = "INSERT INTO activity (jd,area,type,value) VALUES (:jd,:area,:type,:value)";
   
   $stmt = $PDO->prepare($sql);

   $stmt->bindParam(':jd',$jd);
   $stmt->bindParam(':area',$area);
   $stmt->bindParam(':type',$type);
   $stmt->bindParam(':value',$value);

   if($stmt->execute()) {
      
      echo "Sucessful SQL Insertion";
   
   } else {
      echo "SQL Insert Failed.";
}
?>
As I said, if I copy the GET request line from the serial terminal screen that is monitoring the running application and paste it on a web browser it will successfully insert the data in the SQL database as per php code above.

If this is not enough I will assemble the prototype again and post the exact console screens for reference.

By the way, no crash, the ESP32 sketch keeps trying indefinitely to post the GET request and keeps getting BAD REQUEST as response.

Thanks again
Paulo

Who is online

Users browsing this forum: No registered users and 77 guests