Skip to content

Commit 5a861a0

Browse files
committed
Updating example
1 parent 5dbf4f7 commit 5a861a0

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

examples/HTTPMethods/HTTPMethods.ino

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

3030
static AsyncWebServer server(80);
3131

32+
// https://github.com/ESP32Async/ESPAsyncWebServer/issues/404
33+
static void handlePostTest(AsyncWebServerRequest *req, JsonVariant &json) {
34+
AsyncWebServerResponse *response;
35+
Serial.printf("HTTP method: %u\n", req->method());
36+
Serial.printf("WebRequestMethod::HTTP_POST: %u\n", WebRequestMethod::HTTP_POST);
37+
if (req->method() == WebRequestMethod::HTTP_POST) {
38+
response = req->beginResponse(200, "application/json", "{\"msg\": \"OK\"}");
39+
} else {
40+
response = req->beginResponse(501, "application/json", "{\"msg\": \"Not Implemented\"}");
41+
}
42+
req->send(response);
43+
}
44+
3245
void setup() {
3346
Serial.begin(115200);
3447

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

64+
// curl -v http://192.168.4.1/test => Not Implemented
65+
// curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/test => OK
66+
AsyncCallbackJsonWebHandler *testHandler = new AsyncCallbackJsonWebHandler("/test", handlePostTest);
67+
server.addHandler(testHandler);
68+
5169
server.begin();
5270
}
5371

0 commit comments

Comments
 (0)