| 2 |
raymond |
1 |
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
2 |
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
|
|
|
3 |
|
|
|
4 |
//
|
|
|
5 |
// Query parameters and body parameters
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
#include <Arduino.h>
|
|
|
9 |
#if defined(ESP32) || defined(LIBRETINY)
|
|
|
10 |
#include <AsyncTCP.h>
|
|
|
11 |
#include <WiFi.h>
|
|
|
12 |
#elif defined(ESP8266)
|
|
|
13 |
#include <ESP8266WiFi.h>
|
|
|
14 |
#include <ESPAsyncTCP.h>
|
|
|
15 |
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
|
|
|
16 |
#include <RPAsyncTCP.h>
|
|
|
17 |
#include <WiFi.h>
|
|
|
18 |
#endif
|
|
|
19 |
|
|
|
20 |
#include <ESPAsyncWebServer.h>
|
|
|
21 |
|
|
|
22 |
static AsyncWebServer server(80);
|
|
|
23 |
|
|
|
24 |
static const char *htmlContent PROGMEM = R"(
|
|
|
25 |
<!DOCTYPE html>
|
|
|
26 |
<html>
|
|
|
27 |
<head>
|
|
|
28 |
<title>POST Request with Multiple Parameters</title>
|
|
|
29 |
</head>
|
|
|
30 |
<body>
|
|
|
31 |
<form action="http://192.168.4.1" method="POST">
|
|
|
32 |
<label for="who">Who?</label>
|
|
|
33 |
<input type="text" id="who" name="who" value="Carl"><br>
|
|
|
34 |
<label for="g0">g0:</label>
|
|
|
35 |
<input type="text" id="g0" name="g0" value="1"><br>
|
|
|
36 |
<label for="a0">a0:</label>
|
|
|
37 |
<input type="text" id="a0" name="a0" value="2"><br>
|
|
|
38 |
<label for="n0">n0:</label>
|
|
|
39 |
<input type="text" id="n0" name="n0" value="3"><br>
|
|
|
40 |
<label for="t10">t10:</label>
|
|
|
41 |
<input type="text" id="t10" name="t10" value="3"><br>
|
|
|
42 |
<label for="t20">t20:</label>
|
|
|
43 |
<input type="text" id="t20" name="t20" value="4"><br>
|
|
|
44 |
<label for="t30">t30:</label>
|
|
|
45 |
<input type="text" id="t30" name="t30" value="5"><br>
|
|
|
46 |
<label for="t40">t40:</label>
|
|
|
47 |
<input type="text" id="t40" name="t40" value="6"><br>
|
|
|
48 |
<label for="t50">t50:</label>
|
|
|
49 |
<input type="text" id="t50" name="t50" value="7"><br>
|
|
|
50 |
<label for="g1">g1:</label>
|
|
|
51 |
<input type="text" id="g1" name="g1" value="2"><br>
|
|
|
52 |
<label for="a1">a1:</label>
|
|
|
53 |
<input type="text" id="a1" name="a1" value="2"><br>
|
|
|
54 |
<label for="n1">n1:</label>
|
|
|
55 |
<input type="text" id="n1" name="n1" value="3"><br>
|
|
|
56 |
<label for="t11">t11:</label>
|
|
|
57 |
<input type="text" id="t11" name="t11" value="13"><br>
|
|
|
58 |
<label for="t21">t21:</label>
|
|
|
59 |
<input type="text" id="t21" name="t21" value="14"><br>
|
|
|
60 |
<label for="t31">t31:</label>
|
|
|
61 |
<input type="text" id="t31" name="t31" value="15"><br>
|
|
|
62 |
<label for="t41">t41:</label>
|
|
|
63 |
<input type="text" id="t41" name="t41" value="16"><br>
|
|
|
64 |
<label for="t51">t51:</label>
|
|
|
65 |
<input type="text" id="t51" name="t51" value="17"><br>
|
|
|
66 |
<input type="submit" value="Submit">
|
|
|
67 |
</form>
|
|
|
68 |
</body>
|
|
|
69 |
</html>
|
|
|
70 |
)";
|
|
|
71 |
|
|
|
72 |
static const size_t htmlContentLength = strlen_P(htmlContent);
|
|
|
73 |
|
|
|
74 |
void setup() {
|
|
|
75 |
Serial.begin(115200);
|
|
|
76 |
|
|
|
77 |
#if ASYNCWEBSERVER_WIFI_SUPPORTED
|
|
|
78 |
WiFi.mode(WIFI_AP);
|
|
|
79 |
WiFi.softAP("esp-captive");
|
|
|
80 |
#endif
|
|
|
81 |
|
|
|
82 |
// Get query parameters
|
|
|
83 |
//
|
|
|
84 |
// curl -v http://192.168.4.1/?who=Bob
|
|
|
85 |
//
|
|
|
86 |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
|
|
87 |
if (request->hasParam("who")) {
|
|
|
88 |
Serial.printf("Who? %s\n", request->getParam("who")->value().c_str());
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
request->send(200, "text/html", (uint8_t *)htmlContent, htmlContentLength);
|
|
|
92 |
});
|
|
|
93 |
|
|
|
94 |
// Get form body parameters
|
|
|
95 |
//
|
|
|
96 |
// curl -v -H "Content-Type: application/x-www-form-urlencoded" -d "who=Carl" -d "param=value" http://192.168.4.1/
|
|
|
97 |
//
|
|
|
98 |
server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
|
|
|
99 |
// display params
|
|
|
100 |
size_t count = request->params();
|
|
|
101 |
for (size_t i = 0; i < count; i++) {
|
|
|
102 |
const AsyncWebParameter *p = request->getParam(i);
|
|
|
103 |
Serial.printf("PARAM[%u]: %s = %s\n", i, p->name().c_str(), p->value().c_str());
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// get who param
|
|
|
107 |
String who;
|
|
|
108 |
if (request->hasParam("who", true)) {
|
|
|
109 |
who = request->getParam("who", true)->value();
|
|
|
110 |
} else {
|
|
|
111 |
who = "No message sent";
|
|
|
112 |
}
|
|
|
113 |
request->send(200, "text/plain", "Hello " + who + "!");
|
|
|
114 |
});
|
|
|
115 |
|
|
|
116 |
server.begin();
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
// not needed
|
|
|
120 |
void loop() {
|
|
|
121 |
delay(100);
|
|
|
122 |
}
|