mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-10-01 08:40:54 +02:00
add josn example
This commit is contained in:
@@ -552,18 +552,6 @@ void setup() {
|
|||||||
#if __has_include("ArduinoJson.h")
|
#if __has_include("ArduinoJson.h")
|
||||||
// JSON
|
// JSON
|
||||||
|
|
||||||
// receives JSON and sends JSON
|
|
||||||
jsonHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) {
|
|
||||||
// JsonObject jsonObj = json.as<JsonObject>();
|
|
||||||
// ...
|
|
||||||
|
|
||||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
|
||||||
JsonObject root = response->getRoot().to<JsonObject>();
|
|
||||||
root["hello"] = "world";
|
|
||||||
response->setLength();
|
|
||||||
request->send(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
// sends JSON
|
// sends JSON
|
||||||
// curl -v -X GET http://192.168.4.1/json1
|
// curl -v -X GET http://192.168.4.1/json1
|
||||||
server.on("/json1", HTTP_GET, [](AsyncWebServerRequest* request) {
|
server.on("/json1", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
@@ -579,11 +567,23 @@ void setup() {
|
|||||||
AsyncResponseStream* response = request->beginResponseStream("application/json");
|
AsyncResponseStream* response = request->beginResponseStream("application/json");
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject root = doc.to<JsonObject>();
|
JsonObject root = doc.to<JsonObject>();
|
||||||
root["hello"] = "world";
|
root["foo"] = "bar";
|
||||||
serializeJson(root, *response);
|
serializeJson(root, *response);
|
||||||
request->send(response);
|
request->send(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json2
|
||||||
|
// curl -v -X PUT -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json2
|
||||||
|
jsonHandler->setMethod(HTTP_POST | HTTP_PUT);
|
||||||
|
jsonHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) {
|
||||||
|
serializeJson(json, Serial);
|
||||||
|
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||||
|
JsonObject root = response->getRoot().to<JsonObject>();
|
||||||
|
root["hello"] = json.as<JsonObject>()["name"];
|
||||||
|
response->setLength();
|
||||||
|
request->send(response);
|
||||||
|
});
|
||||||
|
|
||||||
// MessagePack
|
// MessagePack
|
||||||
|
|
||||||
// receives MessagePack and sends MessagePack
|
// receives MessagePack and sends MessagePack
|
||||||
|
Reference in New Issue
Block a user