Hi, I am trying to send and "ON"/"OFF" signal from my client ESP32 to a server ESP32 which then updates an ASYNC web page.
Client Code:
void httpPUTRequest(const char* serverName, bool boolPayload, const char* endpoint) {
HTTPClient http;
char url[100];
snprintf(url, sizeof(url), "%s%s", serverName, endpoint);
http.begin(url);
http.addHeader("Content-Type", "text/plain");
String strPayload = boolPayload ? "ON" : "OFF";
Serial.println("Sending PUT request to: " + String(url));
Serial.println("Payload: " + strPayload);
int httpResponseCode = http.PUT(strPayload);
if (httpResponseCode > 0) {
Serial.print("\n\nHTTP Response Code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("\n\nError code: ");
Serial.println(httpResponseCode);
Serial.println(http.errorToString(httpResponseCode).c_str());
}
Serial.println(String(http.headers()));
http.end();
}
Here is how I call the function:
httpPUTRequest(serverName, ledState, "/update-value");
This is my Serial output:
Sending PUT request to: http://11.1.1.111/update-value
Payload: ON
HTTP Response Code: 400
0
Led Status: ON
Does anyone know how to Serial output the HTTP Request data?
kenb4
July 17, 2024, 9:02pm
2
You could try a service that will echo your request
HTTPClient http;
http.begin("http://httpbin.org/anything/update-value");
http.addHeader("Content-Type", "text/plain");
String strPayload = "ON";
int httpResponseCode = http.PUT(strPayload);
Serial.println(httpResponseCode);
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.print(payload);
}
prints
200
{
"args": {},
"data": "ON",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "identity;q=1,chunked;q=0.1,*;q=0",
"Content-Length": "2",
"Content-Type": "text/plain",
"Host": "httpbin.org",
"User-Agent": "ESP32HTTPClient",
"X-Amzn-Trace-Id": "Root=1-66982672-07908b536efc3190737a5a47"
},
"json": null,
"method": "PUT",
"origin": "50.31.197.73",
"url": "http://httpbin.org/anything/update-value"
}
At first glance, the request is simple enough, and you're getting 400; so apparently the server doesn't like it for some reason. You should get and print the payload as above; it might say why the request is invalid.
http.headers() returns zero because you need to call collectHeaders first, before the PUT, to enumerate the ones you are interested in.
system
Closed
January 13, 2025, 9:02pm
3
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.