summaryrefslogtreecommitdiff
path: root/libraries/ESP_Async_WebServer/examples/Params/Params.ino
blob: 2c438a53917068932ad65fe4d10f86f92cf49964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov

//
// Query parameters and body parameters
//

#include <Arduino.h>
#ifdef ESP32
#include <AsyncTCP.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
#include <RPAsyncTCP.h>
#include <WiFi.h>
#endif

#include <ESPAsyncWebServer.h>

static AsyncWebServer server(80);

static const char *htmlContent PROGMEM = R"(
<!DOCTYPE html>
<html>
<head>
    <title>POST Request with Multiple Parameters</title>
</head>
<body>
    <form action="http://192.168.4.1" method="POST">
        <label for="who">Who?</label>
        <input type="text" id="who" name="who" value="Carl"><br>
        <label for="g0">g0:</label>
        <input type="text" id="g0" name="g0" value="1"><br>
        <label for="a0">a0:</label>
        <input type="text" id="a0" name="a0" value="2"><br>
        <label for="n0">n0:</label>
        <input type="text" id="n0" name="n0" value="3"><br>
        <label for="t10">t10:</label>
        <input type="text" id="t10" name="t10" value="3"><br>
        <label for="t20">t20:</label>
        <input type="text" id="t20" name="t20" value="4"><br>
        <label for="t30">t30:</label>
        <input type="text" id="t30" name="t30" value="5"><br>
        <label for="t40">t40:</label>
        <input type="text" id="t40" name="t40" value="6"><br>
        <label for="t50">t50:</label>
        <input type="text" id="t50" name="t50" value="7"><br>
        <label for="g1">g1:</label>
        <input type="text" id="g1" name="g1" value="2"><br>
        <label for="a1">a1:</label>
        <input type="text" id="a1" name="a1" value="2"><br>
        <label for="n1">n1:</label>
        <input type="text" id="n1" name="n1" value="3"><br>
        <label for="t11">t11:</label>
        <input type="text" id="t11" name="t11" value="13"><br>
        <label for="t21">t21:</label>
        <input type="text" id="t21" name="t21" value="14"><br>
        <label for="t31">t31:</label>
        <input type="text" id="t31" name="t31" value="15"><br>
        <label for="t41">t41:</label>
        <input type="text" id="t41" name="t41" value="16"><br>
        <label for="t51">t51:</label>
        <input type="text" id="t51" name="t51" value="17"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
)";

static const size_t htmlContentLength = strlen_P(htmlContent);

void setup() {
  Serial.begin(115200);

#ifndef CONFIG_IDF_TARGET_ESP32H2
  WiFi.mode(WIFI_AP);
  WiFi.softAP("esp-captive");
#endif

  // Get query parameters
  //
  // curl -v http://192.168.4.1/?who=Bob
  //
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    if (request->hasParam("who")) {
      Serial.printf("Who? %s\n", request->getParam("who")->value().c_str());
    }

    request->send(200, "text/html", (uint8_t *)htmlContent, htmlContentLength);
  });

  // Get form body parameters
  //
  // curl -v -H "Content-Type: application/x-www-form-urlencoded" -d "who=Carl" -d "param=value" http://192.168.4.1/
  //
  server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
    // display params
    size_t count = request->params();
    for (size_t i = 0; i < count; i++) {
      const AsyncWebParameter *p = request->getParam(i);
      Serial.printf("PARAM[%u]: %s = %s\n", i, p->name().c_str(), p->value().c_str());
    }

    // get who param
    String who;
    if (request->hasParam("who", true)) {
      who = request->getParam("who", true)->value();
    } else {
      who = "No message sent";
    }
    request->send(200, "text/plain", "Hello " + who + "!");
  });

  server.begin();
}

// not needed
void loop() {
  delay(100);
}