Hello community,
This is my first post, I am a new user of this IoT world so please I ask you understanding if I make little mistakes.
I am trying to program an ESP32 to upload some sensor readings to an online web server via WiFiClientSecure, with no success. I get to connect with the server but somehow, something is wrong with my request. Due to privacy, I will change my domains name for mydomain.com. My server is equipped with SSL certificates (connection seems to go OK). On the server side I have a .php recieving the data
String post_link = "https://www.mycomain.com/post-data.php";
String host = "mydomain.com";
Serial.println("\nStarting connection to server...");
if (!client.connect(serverName, 443)){
Serial.println("Connection failed!");
} else {
Serial.println("Connected to server!");
// Prepare the data to be uploaded:
String data_upload = "api_key=" + apiKeyValue + "&value1=" + String(value1)
+ "&value2=" + String(value2) + "&value3=" + String(value3);
String request = "POST " + String(post_link) + "HTTP/1.1\r\n" +
"Host: " + String(host) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + String(data_upload.length()) + "\r\n" +
"\r\n" + data_upload;
// Send HTTPS POST request
client.print(request);
}
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
client.stop();
Serial.println("Connection closed");
This is what I get as response:
18:37:54.871 -> Starting connection to server...
18:37:56.112 -> Connected to server!
18:37:56.144 -> headers received
18:37:56.144 -> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
18:37:56.144 -> <html><head>
18:37:56.144 -> <title>400 Bad Request</title>
18:37:56.144 -> </head><body>
18:37:56.144 -> <h1>Bad Request</h1>
18:37:56.144 -> <p>Your browser sent a request that this server could not understand.<br />
18:37:56.144 -> </p>
18:37:56.144 -> <p>Additionally, a 400 Bad Request
18:37:56.144 -> error was encountered while trying to use an ErrorDocument to handle the request.</p>
18:37:56.144 -> </body></html>
18:37:56.180 -> Connection closed
Does someone know whay I can't realize a successful data sending to the server?
I would appreciate your help.
Thanks in advance.