-1

I want to send data from a client device with javascript to the host an esp32 with Access point. I tried almost everyting (xml, fetch) but couldn't get it to work. Client Javascript (is called in function with button):

fetch('/data_endpoint', {
method: 'POST',
headers: {
'Content-Type': 'text/plain' // Set the content type to plain text
},
body: "some data to send" // Send the plain text data
})
.then(response => response.text())
.then(data => {
console.log(data); // You can handle the response from the ESP32 here
})
console.error('Error:', error);
});

Server side Esp32:

server.on("/data_endpoint", HTTP_POST, [](AsyncWebServerRequest *request){
Serial.println("in func: ");              
String data = request->arg("plain");
Serial.println("Received data: " + data);
// Handle the data as needed
request->send(200, "text/plain", "Data received successfully"); });

This is what i get every time. Serial console esp32 with message in func: Received data:

I tried xml, fetch and i can verify the request triggers the server but there is always no data. I have also get requests on the same server and they work fine (a json with fetch).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
J_C
  • 1
  • 3
  • does esp32 WebServer support `request->arg("plain")`? I think only ESP8266WebServer has this to access the body of the request – Juraj Aug 20 '23 at 14:55
  • Every library you installed, it comes with examples, including [this one](https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/examples/simple_server/simple_server.ino#L57-L66). – hcheung Aug 21 '23 at 14:53

0 Answers0