ESP32 Asynch Web Server POST方法不起作用

问题描述

我无法使POST方法与ESP32和Async Web Server一起使用。确切地说,可以识别POST路由,但是主体处理失败。

ESP32是WROOM-32。 POST示例改编自 https://techtutorialsx.com/2017/12/09/esp32-arduino-asynchronous-http-webserver-simple-html/

Web服务器以及ESP32确实可以按照我尝试的其他方式工作。尽管下面的示例没有显示它,但是GET方法可以正常工作。当我尝试处理POST请求的正文时,就会出现问题。我已在代码“此处的代码未执行”中添加了注释,以显示无效的代码显示的是简单形式“ /testform.html”,但是在提交时,POST处理程序的标题部分显示内容类型为“ application / x-www-form-urlencoded”,但没有返回任何内容给Chrome浏览器,用于显示POST正文的打印语句不会执行。

ESP32代码如下(我正在使用Arduino IDE):

#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include "SPIFFS.h"
 
const char* ssid = "xxxxxx";      // Actual SSID & Pw removed
const char* password = "xxxxxx";
AsyncWebServer server(80);
 
void setup() {
  Serial.begin(115200);
 
  WiFi.begin(ssid,password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Initialize SPIFFS
  if(!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
  Serial.println(WiFi.localIP());

  server.on("/testform.html",HTTP_GET,[](AsyncWebServerRequest *request) {
    request->send(SPIFFS,"/testform.html",String(),false);
  });
 
  server.on(
    "/my-handling-form-page",HTTP_POST,[](AsyncWebServerRequest * request) {
        // The following print statements work + removing them makes no difference
        // This is displayed on monitor "Content type::application/x-www-form-urlencoded"
        Serial.print("Content type::");
        Serial.println(request->contentType());
    },// Route handling function
    
    NULL,[](AsyncWebServerRequest * request,uint8_t *data,size_t len,size_t index,size_t total) {

  // *** Code here is NOT executed *** 
    
  for (size_t i = 0; i < len; i++) {
    Serial.write(data[i]);
  }
  Serial.println();
  request->send(200);
  });

  // Start server
  server.begin();
}

void notFound(AsyncWebServerRequest *request) {
  request->send(404,"text/plain","My not found ***** ");
}
 
void loop()`{
  
}

“ testform.htm”网页非常简单,并在浏览器请求时按预期显示

<!DOCTYPE html>
<html>
  <head> 
    <b>Test Form</b>
  </head>

  <body>
    <form action="/my-handling-form-page" method="post">
      <ul>
        <li>
          <label for="name">Name:</label>
          <input type="text" id="name" name="user_name">
        </li>
      </ul>
       
      <li class="button">
        <button type="submit">Send your message</button>
      </li>
    </form>
  </body>
</html>

我希望有人可以找到明显的嘘声,或者给我一些下一步我可以尝试的线索。

解决方法

以下代码有效..

server.on(
    "/post",HTTP_POST,[](AsyncWebServerRequest * request){
        // The following print statements work + removing them makes no difference
        // This is displayed on monitor "Content type::application/x-www-form-urlencoded"
        Serial.print("Content type::");
        Serial.println(request->contentType());
        Serial.println("OFF hit.");
    String message;
    int params = request->params();
    Serial.printf("%d params sent in\n",params);
    for (int i = 0; i < params; i++)
    {
        AsyncWebParameter *p = request->getParam(i);
        if (p->isFile())
        {
            Serial.printf("_FILE[%s]: %s,size: %u",p->name().c_str(),p->value().c_str(),p->size());
        }
        else if (p->isPost())
        {
            Serial.printf("%s: %s \n",p->value().c_str());
        }
        else
        {
            Serial.printf("_GET[%s]: %s",p->value().c_str());
        }
    }
    
    });