|
| 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 Arduino HTTP Methods |
| 6 | +// |
| 7 | + |
| 8 | +#include <Arduino.h> |
| 9 | + |
| 10 | +#if !defined(ESP8266) |
| 11 | +// simulate asyncws project being used with another library using Arduino HTTP Methods |
| 12 | +#include <HTTP_Method.h> |
| 13 | +#endif |
| 14 | + |
| 15 | +#if defined(ESP32) || defined(LIBRETINY) |
| 16 | +#include <AsyncTCP.h> |
| 17 | +#include <WiFi.h> |
| 18 | +#elif defined(ESP8266) |
| 19 | +#include <ESP8266WiFi.h> |
| 20 | +#include <ESPAsyncTCP.h> |
| 21 | +#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) |
| 22 | +#include <RPAsyncTCP.h> |
| 23 | +#include <WiFi.h> |
| 24 | +#endif |
| 25 | + |
| 26 | +#define ASYNCWEBSERVER_NO_GLOBAL_HTTP_METHODS 1 |
| 27 | +#undef HTTP_ANY |
| 28 | +#include <ESPAsyncWebServer.h> |
| 29 | + |
| 30 | +static AsyncWebServer server(80); |
| 31 | + |
| 32 | +void setup() { |
| 33 | + Serial.begin(115200); |
| 34 | + |
| 35 | +#if ASYNCWEBSERVER_WIFI_SUPPORTED |
| 36 | + WiFi.mode(WIFI_AP); |
| 37 | + WiFi.softAP("esp-captive"); |
| 38 | +#endif |
| 39 | + |
| 40 | + // curl -v http://192.168.4.1/get-or-post |
| 41 | + // curl -v -X POST -d "a=b" http://192.168.4.1/get-or-post |
| 42 | + server.on("/get-or-post", WebRequestMethod::HTTP_GET | WebRequestMethod::HTTP_POST, [](AsyncWebServerRequest *request) { |
| 43 | + request->send(200, "text/plain", "Hello"); |
| 44 | + }); |
| 45 | + |
| 46 | + // curl -v http://192.168.4.1/any |
| 47 | + server.on("/any", WebRequestMethod::HTTP_ANY, [](AsyncWebServerRequest *request) { |
| 48 | + request->send(200, "text/plain", "Hello"); |
| 49 | + }); |
| 50 | + |
| 51 | + server.begin(); |
| 52 | +} |
| 53 | + |
| 54 | +// not needed |
| 55 | +void loop() { |
| 56 | + delay(100); |
| 57 | +} |
0 commit comments