// // A simple server implementation showing how to: // * serve static messages // * read GET and POST parameters // * handle missing pages / 404s // #include #ifdef ESP32 #include #include #elif defined(ESP8266) #include #include #elif defined(TARGET_RP2040) #include #include #endif #include #include #include #include #include AsyncWebServer server(80); const char* PARAM_MESSAGE = "message"; void notFound(AsyncWebServerRequest* request) { request->send(404, "text/plain", "Not found"); } AsyncCallbackJsonWebHandler* jsonHandler = new AsyncCallbackJsonWebHandler("/json2"); AsyncCallbackMessagePackWebHandler* msgPackHandler = new AsyncCallbackMessagePackWebHandler("/msgpack2"); void setup() { Serial.begin(115200); #ifndef CONFIG_IDF_TARGET_ESP32H2 // WiFi.mode(WIFI_STA); // WiFi.begin("YOUR_SSID", "YOUR_PASSWORD"); // if (WiFi.waitForConnectResult() != WL_CONNECTED) { // Serial.printf("WiFi Failed!\n"); // return; // } // Serial.print("IP Address: "); // Serial.println(WiFi.localIP()); WiFi.mode(WIFI_AP); WiFi.softAP("esp-captive"); #endif server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(200, "text/plain", "Hello, world"); }); server.on("/file", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/index.html"); }); /* ❯ curl -I -X HEAD http://192.168.4.1/download HTTP/1.1 200 OK Content-Length: 1024 Content-Type: application/octet-stream Connection: close Accept-Ranges: bytes */ // Ref: https://github.com/mathieucarbou/ESPAsyncWebServer/pull/80 server.on("/download", HTTP_HEAD | HTTP_GET, [](AsyncWebServerRequest* request) { if (request->method() == HTTP_HEAD) { AsyncWebServerResponse* response = request->beginResponse(200, "application/octet-stream"); response->setContentLength(1024); // myFile.getSize() response->addHeader("Accept-Ranges", "bytes"); // ... request->send(response); } else { // ... } }); // Send a GET request to /get?message= server.on("/get", HTTP_GET, [](AsyncWebServerRequest* request) { String message; if (request->hasParam(PARAM_MESSAGE)) { message = request->getParam(PARAM_MESSAGE)->value(); } else { message = "No message sent"; } request->send(200, "text/plain", "Hello, GET: " + message); }); // Send a POST request to /post with a form field message set to server.on("/post", HTTP_POST, [](AsyncWebServerRequest* request) { String message; if (request->hasParam(PARAM_MESSAGE, true)) { message = request->getParam(PARAM_MESSAGE, true)->value(); } else { message = "No message sent"; } request->send(200, "text/plain", "Hello, POST: " + message); }); // JSON // receives JSON and sends JSON jsonHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) { JsonObject jsonObj = json.as(); // ... AsyncJsonResponse* response = new AsyncJsonResponse(); JsonObject root = response->getRoot().to(); root["hello"] = "world"; response->setLength(); request->send(response); }); // sends JSON server.on("/json1", HTTP_GET, [](AsyncWebServerRequest* request) { AsyncJsonResponse* response = new AsyncJsonResponse(); JsonObject root = response->getRoot().to(); root["hello"] = "world"; response->setLength(); request->send(response); }); // MessagePack // receives MessagePack and sends MessagePack msgPackHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) { JsonObject jsonObj = json.as(); // ... AsyncMessagePackResponse* response = new AsyncMessagePackResponse(); JsonObject root = response->getRoot().to(); root["hello"] = "world"; response->setLength(); request->send(response); }); // sends MessagePack server.on("/msgpack1", HTTP_GET, [](AsyncWebServerRequest* request) { AsyncMessagePackResponse* response = new AsyncMessagePackResponse(); JsonObject root = response->getRoot().to(); root["hello"] = "world"; response->setLength(); request->send(response); }); server.addHandler(jsonHandler); server.addHandler(msgPackHandler); server.onNotFound(notFound); server.begin(); } void loop() { }