Skip to content

Commit

Permalink
doc: Update example for issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jan 22, 2025
1 parent 22fdad1 commit 65e9177
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
22 changes: 17 additions & 5 deletions examples/SimpleServer/SimpleServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ const char* SSE_HTLM PROGMEM = R"(
</html>
)";

void notFound(AsyncWebServerRequest* request) {
request->send(404, "text/plain", "Not found");
}

#if __has_include("ArduinoJson.h")
AsyncCallbackJsonWebHandler* jsonHandler = new AsyncCallbackJsonWebHandler("/json2");
AsyncCallbackMessagePackWebHandler* msgPackHandler = new AsyncCallbackMessagePackWebHandler("/msgpack2");
Expand Down Expand Up @@ -759,7 +755,23 @@ websocat: error running
server.addHandler(msgPackHandler);
#endif

server.onNotFound(notFound);
// catch any request, and send a 404 Not Found response
// except for /game_log which is handled by onRequestBody
server.onNotFound([](AsyncWebServerRequest* request) {
if (request->url() == "/game_log")
return; // response object already creted by onRequestBody

request->send(404, "text/plain", "Not found");
});

// https://github.com/ESP32Async/ESPAsyncWebServer/issues/6
// curl -v -X POST http://192.168.4.1/game_log -H "Content-Type: application/json" -d '{"game": "test"}'
// catch any POST request and send a 200 OK response
server.onRequestBody([](AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) {
if (request->url() == "/game_log") {
request->send(200, "application/json", "{\"status\":\"OK\"}");
}
});

server.begin();
}
Expand Down
1 change: 1 addition & 0 deletions src/WebHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ void AsyncCallbackWebHandler::handleUpload(AsyncWebServerRequest* request, const
_onUpload(request, filename, index, data, len, final);
}
void AsyncCallbackWebHandler::handleBody(AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) {
// ESP_LOGD("AsyncWebServer", "AsyncCallbackWebHandler::handleBody");
if (_onBody)
_onBody(request, data, len, index, total);
}
1 change: 1 addition & 0 deletions src/WebRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void AsyncWebServerRequest::_onData(void* buf, size_t len) {
}
}
if (!_isPlainPost) {
// ESP_LOGD("AsyncWebServer", "_isPlainPost: %d, _handler: %p", _isPlainPost, _handler);
if (_handler)
_handler->handleBody(this, (uint8_t*)buf, len, _parsedLength, _contentLength);
_parsedLength += len;
Expand Down
2 changes: 1 addition & 1 deletion src/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void AsyncWebServer::_attachHandler(AsyncWebServerRequest* request) {
return;
}
}

// ESP_LOGD("AsyncWebServer", "No handler found for %s, using _catchAllHandler pointer: %p", request->url().c_str(), _catchAllHandler);
request->setHandler(_catchAllHandler);
}

Expand Down

0 comments on commit 65e9177

Please sign in to comment.