Skip to content

[BUG] Cant send response onRequestBodyfrom #6

@phoenixofhp

Description

@phoenixofhp
  • ESP32 Arduino Core version: 3.07 based on ESP-IDF v5.1.4+
  • Which board: ESP32-WROOM-32D
  • Which platform (ESP8266, ESP32): ESP32
  • AsyncTCP version (if applicable): 3.3.2
  • AsyncTCP repository location you use (if applicable and if you use one coming from elsewhere): https://github.com/mathieucarbou/AsyncTCP
  • Do you use AsyncTCPSock (y/n): n

Cant send response from onRequestBodyfrom

  server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
      if (request->url() == "/game_log" && request->method() == HTTP_POST) {
          handleGameLog(request, data, len);
      }
  });

void handleGameLog(AsyncWebServerRequest *request, uint8_t *data, size_t len) {
    String body = "";
    body.reserve(len);
    
    for (size_t i = 0; i < len; i++) {
        body += (char)data[i];
    }

    JsonDocument doc;
    DeserializationError error = deserializeJson(doc, body);
    if (error) {
        Serial.println(error.c_str());
        request->send(400, "application/json", "{\"error\":\"Bad format JSON.\"}");
        return;
    }

    String game = doc["game"] | "";
    String token = doc["token"] | "";
    String result = doc["result"] | "";
    String time = doc["time"] | "";
    String name = doc["name"] | "";

    if (game.isEmpty() || token.isEmpty() || result.isEmpty() || time.isEmpty() || name.isEmpty()) {
        request->send(400, "application/json", "{\"error\":\"Not enough data\"}");
        return;
    }

    if (!validateAndRemoveToken(token, game)) {
        request->send(401, "application/json", "{\"error\":\"Bad token.\"}");
        return;
    }

  /* Some logic */

    request->send(200, "application/json", "{\"status\":\"OK\"}");
}
PS > curl -X POST http://10.10.32.190/game_log -H "Content-Type: application/json" -d '{"game": "test","token": "5d59f64be4888f7e","result": "win","time": "120"}'
PS >

Metadata

Metadata

Assignees

Labels

Type: QuestionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions