Skip to content

Commit c515c2a

Browse files
Merge pull request #412 from ESP32Async/example-update
Update HTTPMethods example to add an example for Json handler taken from issue 404
2 parents 5dbf4f7 + cd7a78a commit c515c2a

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

examples/HTTPMethods/HTTPMethods.ino

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929

3030
static AsyncWebServer server(80);
3131

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+
3245
void setup() {
3346
Serial.begin(115200);
3447

@@ -48,6 +61,13 @@ void setup() {
4861
request->send(200, "text/plain", "Hello");
4962
});
5063

64+
#if ASYNC_JSON_SUPPORT == 1
65+
// curl -v http://192.168.4.1/test => Not Implemented
66+
// curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/test => OK
67+
AsyncCallbackJsonWebHandler *testHandler = new AsyncCallbackJsonWebHandler("/test", handlePostTest);
68+
server.addHandler(testHandler);
69+
#endif
70+
5171
server.begin();
5272
}
5373

0 commit comments

Comments
 (0)