Skip to content

Commit 4341ec7

Browse files
committed
Updated examples to show teh difference between using Arduino Core include and ESP-IDf include
1 parent a3921bf commit 4341ec7

3 files changed

Lines changed: 121 additions & 5 deletions

File tree

examples/HTTPMethods/HTTPMethods.ino renamed to examples/HTTPMethodsWithArduino/HTTPMethodsWithArduino.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
33

44
//
5-
// HTTP Method usage example and check compatibility with Arduino HTTP Methods
5+
// HTTP Method usage example and check compatibility with Arduino HTTP Methods when HTTP_Method.h is included.
6+
// ESP-IDf enums are imported, and HTTP_ANY is defined by Arduino Core.
7+
// In that case, we cannot use directly asyncws enums: we have to namespace them.
8+
// Also, asycnws HTTP_ANY is not available sine already defined by Arduino as a macro.
9+
// So we have to use AsyncWebRequestMethod::HTTP_ALL instead of HTTP_ANY in that case.
610
//
711

812
#include <Arduino.h>
913

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

@@ -79,8 +82,8 @@ void setup() {
7982
request->send(200, "text/plain", "Hello");
8083
});
8184

82-
// curl -v http://192.168.4.1/any
83-
server.on("/any", AsyncWebRequestMethod::HTTP_ALL, [](AsyncWebServerRequest *request) {
85+
// curl -v http://192.168.4.1/all
86+
server.on("/all", AsyncWebRequestMethod::HTTP_ALL, [](AsyncWebServerRequest *request) {
8487
request->send(200, "text/plain", "Hello");
8588
});
8689

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
3+
4+
//
5+
// HTTP Method usage example and check compatibility with ESP-IDF HTTP Methods when http_parser.h is included.
6+
// ESP-IDF enums are imported, and HTTP_ANY is NOT defined by ESP-IDF.
7+
// So asyncws is able to define it.
8+
// We cannot use directly other asyncws enums to avoid conflicts with ESP-IDF: we have to namespace them.
9+
//
10+
11+
#include <Arduino.h>
12+
13+
#if !defined(ESP8266)
14+
#include "http_parser.h"
15+
#endif
16+
17+
#if defined(ESP32) || defined(LIBRETINY)
18+
#include <AsyncTCP.h>
19+
#include <WiFi.h>
20+
#elif defined(ESP8266)
21+
#include <ESP8266WiFi.h>
22+
#include <ESPAsyncTCP.h>
23+
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
24+
#include <RPAsyncTCP.h>
25+
#include <WiFi.h>
26+
#endif
27+
28+
#include <ESPAsyncWebServer.h>
29+
30+
static AsyncWebServer server(80);
31+
32+
#if ASYNC_JSON_SUPPORT == 1
33+
// https://github.com/ESP32Async/ESPAsyncWebServer/issues/404
34+
static void handlePostTest(AsyncWebServerRequest *req, JsonVariant &json) {
35+
AsyncWebServerResponse *response;
36+
if (req->method() == WebRequestMethod::HTTP_POST) {
37+
response = req->beginResponse(200, "application/json", "{\"msg\": \"OK\"}");
38+
} else {
39+
response = req->beginResponse(501, "application/json", "{\"msg\": \"Not Implemented\"}");
40+
}
41+
req->send(response);
42+
}
43+
#endif
44+
45+
// user defined functions that turns out to have similar signatures to the ones in global header
46+
int stringToMethod(const String &) {
47+
return 0;
48+
}
49+
const char *methodToString(WebRequestMethod) {
50+
return "METHOD";
51+
}
52+
bool methodMatches(WebRequestMethodComposite c, WebRequestMethod m) {
53+
return false;
54+
};
55+
56+
static WebRequestMethodComposite allowed = AsyncWebRequestMethod::HTTP_HEAD | AsyncWebRequestMethod::HTTP_OPTIONS;
57+
58+
class MyRequestHandler : public AsyncWebHandler {
59+
public:
60+
bool canHandle(__unused AsyncWebServerRequest *request) const override {
61+
// Test backward compatibility with previous way of checking if a method is allowed in a composite using a bit operator
62+
return allowed & request->method();
63+
}
64+
65+
void handleRequest(AsyncWebServerRequest *request) override {
66+
request->send(200, "text/plain", "Hello from custom handler");
67+
}
68+
};
69+
70+
void setup() {
71+
Serial.begin(115200);
72+
73+
#if ASYNCWEBSERVER_WIFI_SUPPORTED
74+
WiFi.mode(WIFI_AP);
75+
WiFi.softAP("esp-captive");
76+
#endif
77+
78+
// curl -v http://192.168.4.1/get-or-post
79+
// curl -v -X POST -d "a=b" http://192.168.4.1/get-or-post
80+
server.on("/get-or-post", AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST, [](AsyncWebServerRequest *request) {
81+
request->send(200, "text/plain", "Hello");
82+
});
83+
84+
// curl -v http://192.168.4.1/all
85+
server.on("/all", AsyncWebRequestMethod::HTTP_ALL, [](AsyncWebServerRequest *request) {
86+
request->send(200, "text/plain", "Hello");
87+
});
88+
89+
// will show a deprecation warning
90+
server.on("/any", AsyncWebRequestMethod::HTTP_ANY, [](AsyncWebServerRequest *request) {
91+
request->send(200, "text/plain", "Hello");
92+
});
93+
94+
#if ASYNC_JSON_SUPPORT == 1
95+
// curl -v http://192.168.4.1/test => Not Implemented
96+
// curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/test => OK
97+
AsyncCallbackJsonWebHandler *testHandler = new AsyncCallbackJsonWebHandler("/test", handlePostTest);
98+
server.addHandler(testHandler);
99+
#endif
100+
101+
// curl -v -X HEAD http://192.168.4.1/custom => answers
102+
// curl -v -X OPTIONS http://192.168.4.1/custom => answers
103+
// curl -v -X POST http://192.168.4.1/custom => no answer
104+
server.addHandler(new MyRequestHandler());
105+
106+
server.begin();
107+
}
108+
109+
// not needed
110+
void loop() {
111+
delay(100);
112+
}

platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ lib_dir = .
1515
; src_dir = examples/FlashResponse
1616
; src_dir = examples/HeaderManipulation
1717
; src_dir = examples/Headers
18-
; src_dir = examples/HTTPMethods
18+
; src_dir = examples/HTTPMethodsWithArduino
19+
; src_dir = examples/HTTPMethodsWithESPIDF
1920
; src_dir = examples/Json
2021
; src_dir = examples/LargeResponse
2122
; src_dir = examples/Logging

0 commit comments

Comments
 (0)