I'm attempting to send a POST request to a site that I've set up, using an ESP32, containing 2 pieces of data. I have attempted to use the site "reqbin.com" to send a POST request containing the same data, so I don't think that the data itself is the issue.
Below is my code:
char ssid[] = "{{name}}";
char pass[] = "{{pass}}";
int port = 8080;
WiFiClient wifi;
void setup()
{
delay(1000);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, pass); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
String serverAddress = "sitename.com/data.php";
String contentType = "text/plain"
String postData = "t=26.1 h=25.6";
HttpClient http = HttpClient(wifi, serverAddress, port);
http.beginRequest(); //Specify request destination
int httpCode = http.post("/", contentType, postData);
Serial.println("httpCode = " + httpCode);
if (httpCode > 0)
{
String response = http.responseBody();
Serial.println("httpCode === " + httpCode);
Serial.println(response);
}
else
{
Serial.println("Error on POST request");
Serial.println(httpCode);
}
http.endRequest();
}
When using reqbin, I get a Status 200, and the site returns. However, on the ESP32, it returns the error:
[E][WiFiGeneric.cpp:654] hostByName(): DNS Failed for sitename.com/data.php
I am unsure if the syntax of my request is incorrect.
UPDATE: Changed my loop and changed the server address to this:
String serverAddress = "sitename.com";
String contentType = "text/plain";
String postData = "t=26.1 h=25.6";
HttpClient http = HttpClient(wifi, serverAddress, port);
http.beginRequest(); //Specify request destination
int httpCode = http.post("/data.php", contentType, postData);
Serial.println("httpCode = " + httpCode);
Now getting the error:
socket error on fd 57, errno: 104, "Connection reset by peer"
Edit for Codebreaker:
String serverAddress = "sitename.com";
HttpClient http = HttpClient(wifi, serverAddress, port);
String response;
int statusCode = 0;
Setup has stayed the same.
void loop()
{
String contentType = "application/x-www-form-urlencoded";
// String postData = temp + " " + humid;
String postData = "?t=26.1&h=25.6";
http.post("/data.php", contentType, postData);
// read the status code and body of the response
statusCode = http.responseStatusCode();
response = http.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
}
I guess the server expects url encoded data and not text plain. So try define the following before setup()
int port = 80;
String response;
int statusCode = 0;
String serverAddress = "sitename.com";
HttpClient http = HttpClient(wifi, serverAddress, port);
And in your loop you build the post as follows
String contentType = "application/x-www-form-urlencoded";
String postData = "?t=26.1&h=25.6";
http.post("/data.php", contentType, postData);
and than read the response
// read the status code and body of the response
statusCode = http.responseStatusCode();
response = http.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
If that works replace String class with char arrays like this
char serverAddress[] = "www.test.com"; // server address
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.