Skip to content

Commit 661f503

Browse files
Merge pull request esphome#8 from ESP32Async/issue-6
doc: Update example for issue esphome#6
2 parents 22fdad1 + 65e9177 commit 661f503

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

examples/SimpleServer/SimpleServer.ino

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,6 @@ const char* SSE_HTLM PROGMEM = R"(
183183
</html>
184184
)";
185185

186-
void notFound(AsyncWebServerRequest* request) {
187-
request->send(404, "text/plain", "Not found");
188-
}
189-
190186
#if __has_include("ArduinoJson.h")
191187
AsyncCallbackJsonWebHandler* jsonHandler = new AsyncCallbackJsonWebHandler("/json2");
192188
AsyncCallbackMessagePackWebHandler* msgPackHandler = new AsyncCallbackMessagePackWebHandler("/msgpack2");
@@ -759,7 +755,23 @@ websocat: error running
759755
server.addHandler(msgPackHandler);
760756
#endif
761757

762-
server.onNotFound(notFound);
758+
// catch any request, and send a 404 Not Found response
759+
// except for /game_log which is handled by onRequestBody
760+
server.onNotFound([](AsyncWebServerRequest* request) {
761+
if (request->url() == "/game_log")
762+
return; // response object already creted by onRequestBody
763+
764+
request->send(404, "text/plain", "Not found");
765+
});
766+
767+
// https://github.com/ESP32Async/ESPAsyncWebServer/issues/6
768+
// curl -v -X POST http://192.168.4.1/game_log -H "Content-Type: application/json" -d '{"game": "test"}'
769+
// catch any POST request and send a 200 OK response
770+
server.onRequestBody([](AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) {
771+
if (request->url() == "/game_log") {
772+
request->send(200, "application/json", "{\"status\":\"OK\"}");
773+
}
774+
});
763775

764776
server.begin();
765777
}

src/WebHandlers.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ void AsyncCallbackWebHandler::handleUpload(AsyncWebServerRequest* request, const
308308
_onUpload(request, filename, index, data, len, final);
309309
}
310310
void AsyncCallbackWebHandler::handleBody(AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) {
311+
// ESP_LOGD("AsyncWebServer", "AsyncCallbackWebHandler::handleBody");
311312
if (_onBody)
312313
_onBody(request, data, len, index, total);
313314
}

src/WebRequest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void AsyncWebServerRequest::_onData(void* buf, size_t len) {
141141
}
142142
}
143143
if (!_isPlainPost) {
144+
// ESP_LOGD("AsyncWebServer", "_isPlainPost: %d, _handler: %p", _isPlainPost, _handler);
144145
if (_handler)
145146
_handler->handleBody(this, (uint8_t*)buf, len, _parsedLength, _contentLength);
146147
_parsedLength += len;

src/WebServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void AsyncWebServer::_attachHandler(AsyncWebServerRequest* request) {
153153
return;
154154
}
155155
}
156-
156+
// ESP_LOGD("AsyncWebServer", "No handler found for %s, using _catchAllHandler pointer: %p", request->url().c_str(), _catchAllHandler);
157157
request->setHandler(_catchAllHandler);
158158
}
159159

0 commit comments

Comments
 (0)