Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions examples/HTTPMethods/HTTPMethods.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles

//
// HTTP Method usage example and check compatibility with Arduino HTTP Methods
//

#include <Arduino.h>

#if !defined(ESP8266)
// simulate asyncws project being used with another library using Arduino HTTP Methods
#include <HTTP_Method.h>
#endif

#if defined(ESP32) || defined(LIBRETINY)
#include <AsyncTCP.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
#include <RPAsyncTCP.h>
#include <WiFi.h>
#endif

#include <ESPAsyncWebServer.h>

static AsyncWebServer server(80);

void setup() {
Serial.begin(115200);

#if ASYNCWEBSERVER_WIFI_SUPPORTED
WiFi.mode(WIFI_AP);
WiFi.softAP("esp-captive");
#endif

// curl -v http://192.168.4.1/get-or-post => Hello
// curl -v -X POST -d "a=b" http://192.168.4.1/get-or-post => Hello
// curl -v -X PUT -d "a=b" http://192.168.4.1/get-or-post => 404
// curl -v -X PATCH -d "a=b" http://192.168.4.1/get-or-post => 404
server.on("/get-or-post", HTTP_GET | HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hello");
});

// curl -v http://192.168.4.1/any => Hello
// curl -v -X POST -d "a=b" http://192.168.4.1/any => Hello
// curl -v -X PUT -d "a=b" http://192.168.4.1/any => Hello
// curl -v -X PATCH -d "a=b" http://192.168.4.1/any => Hello
server.on("/any", HTTP_ANY, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hello");
});

server.begin();
}

// not needed
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ lib_dir = .
; src_dir = examples/Filters
; src_dir = examples/FlashResponse
; src_dir = examples/HeaderManipulation
; src_dir = examples/Headers
; src_dir = examples/HTTPMethods
; src_dir = examples/Json
; src_dir = examples/LargeResponse
; src_dir = examples/Logging
Expand All @@ -38,9 +40,9 @@ src_dir = examples/PerfTests
; src_dir = examples/UploadFlash
; src_dir = examples/URIMatcher
; src_dir = examples/URIMatcherTest
; src_dir = examples/WebDAVMethods
; src_dir = examples/WebSocket
; src_dir = examples/WebSocketEasy
; src_dir = examples/WebDAVMethods

[env]
framework = arduino
Expand Down
15 changes: 6 additions & 9 deletions src/AsyncJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,11 @@ size_t AsyncMessagePackResponse::_fillBuffer(uint8_t *data, size_t len) {

#if ARDUINOJSON_VERSION_MAJOR == 6
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize)
: _uri(std::move(uri)),
_method(AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST | AsyncWebRequestMethod::HTTP_PUT | AsyncWebRequestMethod::HTTP_PATCH),
_onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
: _uri(std::move(uri)), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize),
_maxContentLength(16384) {}
#else
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, ArJsonRequestHandlerFunction onRequest)
: _uri(std::move(uri)),
_method(AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST | AsyncWebRequestMethod::HTTP_PUT | AsyncWebRequestMethod::HTTP_PATCH),
_onRequest(onRequest), _maxContentLength(16384) {}
: _uri(std::move(uri)), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
#endif

bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) const {
Expand All @@ -135,17 +132,17 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) cons
}

#if ASYNC_MSG_PACK_SUPPORT == 1
return request->method() == AsyncWebRequestMethod::HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json)
return request->method() == HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json)
|| request->contentType().equalsIgnoreCase(asyncsrv::T_application_msgpack);
#else
return request->method() == AsyncWebRequestMethod::HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json);
return request->method() == HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json);
#endif
}

void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest *request) {
if (_onRequest) {
// GET request:
if (request->method() == AsyncWebRequestMethod::HTTP_GET) {
if (request->method() == HTTP_GET) {
JsonVariant json;
_onRequest(request, json);
return;
Expand Down
Loading
Loading