0

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

4
  • Shouldn't you check if deserializeJson succeeded and that the resulting object has a name field before accessing it? Commented Apr 30, 2020 at 11:43
  • no, the deserialization does not run checked it already as soon as request->value().c_str() this is hit the error occurs @Botje Commented Apr 30, 2020 at 12:00
  • request->getParam("plain",true) returns nullptr if the mentioned parameter is not present. Always check your assumptions request->hasParam("plain", true)) and return values! Commented Apr 30, 2020 at 12:26
  • yes thank you it worked the param was empty Commented May 13, 2020 at 5:37

1 Answer 1

0

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.     
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.