ESP32 WIFI webclient returns 400 Bad Request
Posted: 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
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);
}