Skip to content

Commit aab445a

Browse files
committed
Put QUERY on its own Json route and document the IDF enum clash
The 8-byte edge-case handler made the QUERY curl a 413, and WebDAV is the wrong home. Use AsyncWebRequestMethod::HTTP_QUERY when http_parser.h is included so platform enumerator 33 is not treated as a bit mask.
1 parent 43e996b commit aab445a

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

docs/requests.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ Platform `http_method` mapping is compiled in only when the platform parser defi
2323
Register it like any other verb:
2424

2525
```cpp
26-
server.on("/search", HTTP_QUERY, [](AsyncWebServerRequest *request) {
26+
server.on("/search", AsyncWebRequestMethod::HTTP_QUERY, [](AsyncWebServerRequest *request) {
2727
AsyncWebServerResponse *res = request->beginResponse(200, "text/plain", "ok");
28-
res->addHeader("Accept-Query", "application/json");
28+
res->addHeader(asyncsrv::T_Accept_Query, "application/json");
2929
request->send(res);
3030
});
3131

3232
auto *json = new AsyncCallbackJsonWebHandler("/search", [](AsyncWebServerRequest *request, JsonVariant &json) {
3333
request->send(200, "application/json", "{\"ok\":true}");
3434
});
35-
json->setMethod(HTTP_QUERY);
35+
json->setMethod(AsyncWebRequestMethod::HTTP_QUERY);
3636
server.addHandler(json);
3737
```
3838
39+
Use `AsyncWebRequestMethod::HTTP_QUERY` when `http_parser.h` is included: the platform enumerator `HTTP_QUERY` is `33`, not the library bit flag.
40+
3941
### Headers
4042
4143
```cpp

examples/arduino/HTTPMethodsWithESPIDF/HTTPMethodsWithESPIDF.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ void setup() {
119119
assert(composite3 == composite4);
120120
assert(composite1 != composite3);
121121
assert(composite5 == AsyncWebRequestMethod::HTTP_GET);
122+
123+
#if defined(HTTP_PARSER_HAS_QUERY)
124+
// http_parser.h is included above, so HTTP_QUERY is the platform enumerator
125+
// (33), not the Async bit. The integration ctor must map it.
126+
static_assert(WebRequestMethodComposite(HTTP_QUERY).matches(AsyncWebRequestMethod::HTTP_QUERY), "platform HTTP_QUERY must map onto AsyncWebRequestMethod::HTTP_QUERY");
127+
#endif
122128
}
123129

124130
// not needed

examples/arduino/Json/Json.ino

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ void setup() {
6363

6464
// curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json2
6565
// curl -v -X PUT -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json2
66-
// curl -v -X QUERY -H 'Content-Type: application/json' -d '{"q":"You"}' http://192.168.4.1/json2
6766
//
6867
// edge cases:
6968
//
@@ -75,7 +74,7 @@ void setup() {
7574
// curl -v -X POST -H "Content-Type: application/json" -d "123456789" -H "Content-Length: 8" http://192.168.4.1/json2 => 12345678
7675
// curl -v -X POST -H "Content-Type: application/json" -d "123456789" -H "Content-Length: 9" http://192.168.4.1/json2 => 413: Content length exceeds maximum allowed
7776
handler->setMaxContentLength(8);
78-
handler->setMethod(HTTP_POST | HTTP_PUT | HTTP_QUERY);
77+
handler->setMethod(HTTP_POST | HTTP_PUT);
7978
handler->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) {
8079
serializeJson(json, Serial);
8180
Serial.println();
@@ -88,6 +87,22 @@ void setup() {
8887

8988
server.addHandler(handler);
9089

90+
// RFC 10008 QUERY: same JSON body path as POST/PUT. Not enabled on
91+
// AsyncCallbackJsonWebHandler by default; call setMethod(HTTP_QUERY).
92+
// curl -v -X QUERY -H 'Content-Type: application/json' -d '{"q":1}' http://192.168.4.1/json-query
93+
AsyncCallbackJsonWebHandler *queryHandler = new AsyncCallbackJsonWebHandler("/json-query");
94+
queryHandler->setMethod(HTTP_QUERY);
95+
queryHandler->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) {
96+
serializeJson(json, Serial);
97+
Serial.println();
98+
AsyncJsonResponse *response = new AsyncJsonResponse();
99+
JsonObject root = response->getRoot().to<JsonObject>();
100+
root["hello"] = json.as<JsonObject>()["q"];
101+
response->setLength();
102+
request->send(response);
103+
});
104+
server.addHandler(queryHandler);
105+
91106
// New Json API since 3.8.2, which works for both Json and MessagePack bodies
92107
// curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json3
93108

examples/arduino/WebDAVMethods/WebDAVMethods.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ using namespace asyncsrv;
3131
// curl -s -X MKCOL http://192.168.4.1/
3232
// curl -s -X MOVE http://192.168.4.1/
3333
// curl -s -X COPY http://192.168.4.1/
34-
// curl -s -X QUERY -H 'Content-Type: application/json' -d '{"q":1}' http://192.168.4.1/
3534
//
3635
// In all cases, the request will be accepted with text/plain response 200 like
3736
// "Got method PROPFIND on URL /"

0 commit comments

Comments
 (0)