From 5a841478fc0c3d3f0f008b23bf4a248a0ec08a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20L=C3=B6nn?= Date: Sun, 3 Mar 2024 16:34:19 +0100 Subject: [PATCH 1/2] Add support for HTTP_GET for JsonHndler (so we can use it to make resful apis) --- src/AsyncJson.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index 2fa6a2d26..ffadea4da 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -185,10 +185,10 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler { public: #ifdef ARDUINOJSON_5_COMPATIBILITY AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest) - : _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} + : _uri(uri), _method(HTTP_GET|HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} #else AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMIC_JSON_DOCUMENT_SIZE) - : _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {} + : _uri(uri), _method(HTTP_GET|HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {} #endif void setMethod(WebRequestMethodComposite method){ _method = method; } @@ -199,13 +199,15 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler { if(!_onRequest) return false; - if(!(_method & request->method())) + WebRequestMethodComposite request_method = request->method(); + + if(!(_method & request_method)) return false; if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/"))) return false; - if ( !request->contentType().equalsIgnoreCase(JSON_MIMETYPE) ) + if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(JSON_MIMETYPE) ) return false; request->addInterestingHeader("ANY"); @@ -214,7 +216,12 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler { virtual void handleRequest(AsyncWebServerRequest *request) override final { if(_onRequest) { - if (request->_tempObject != NULL) { + + if (request->method() == HTTP_GET) { + JsonVariant json; + _onRequest(request, json); + return; + } else if (request->_tempObject != NULL) { #ifdef ARDUINOJSON_5_COMPATIBILITY DynamicJsonBuffer jsonBuffer; From c0a9f8e7c82cac7500ae2028802168ec90f95f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20L=C3=B6nn?= Date: Sun, 3 Mar 2024 19:42:27 +0100 Subject: [PATCH 2/2] Make AsyncCallbackJsonWebHandler honor authentication pattern --- src/AsyncJson.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index ffadea4da..3c404d778 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -215,6 +215,9 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler { } virtual void handleRequest(AsyncWebServerRequest *request) override final { + if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str())) + return request->requestAuthentication(); + if(_onRequest) { if (request->method() == HTTP_GET) {