Search code examples
c++esp32

guru meditation error when reading data from ESPAsyncWebServer request parameters


I am working on ESP32, New to this device.

I am currently facing the following error. when I try to read the data from request->value().c_str() the serial monitor shows me guru meditation error error SS of error: enter image description here

My code:

server.on("/register", HTTP_POST, [](AsyncWebServerRequest *request) {
    request->send(200, "text/plain", Register(request->getParam("plain",true)).c_str());
});

The function:

string Register(AsyncWebParameter *request){

  const size_t bufferSize = JSON_OBJECT_SIZE(1) + 250;;
  StaticJsonDocument<bufferSize>  data;
  deserializeJson(data, request->value().c_str()); // facing issue here when request->value().c_str() is called
  const char* name = data["username"];

  return String("Execution complete");
  }

I have return simple javascript function to call the API:

function Register() {
const tosend={
      "username":"Mohit",
      "password":"Mohit10#",
      "lastname":"mhatre",
      "firstname":"Mohit"
  };
  const url="http://192.168.2.120/register";
  
  fetch(url, {
    method: 'post',
    headers: {
      // if you want to set
    },
    body: tosend
  })
    .then(
      function(response) {
        response.json().then(function(data) {
          console.log(data);
        });
      }
    )
    .catch(function(err) {
      console.log('Fetch Error :-S', err);
    });
}

Can someone please help me resolve the issue i am new to this and stuck here from yesterday


Solution

  • I resolved this issue. As described by Botje in the comment above. The problem was that Register(request->getParam("plain",true)).c_str() was returning empty results so when i tried to access them the esp it caused issue.

    Following are the code changes I made:

    Server code:

      server.on("/register", HTTP_POST, [](AsyncWebServerRequest *request){request->send(200, "text/plain", "");}, NULL, Register);
    

    the function

    void Register(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
    {
      chipid=ESP.getEfuseMac();
    
      StaticJsonDocument<200> doc;
      deserializeJson(doc, data);
    
      doc["device_id"] = (uint16_t)(chipid>>32)+(uint32_t)chipid;
    
      char jsonBuffer[512];
      serializeJson(doc, jsonBuffer);
      // used the json as required.     
    }