post - ESP32 异步 Web 服务器 POST 方法不起作用

我无法让 POST 方法与 ESP32 和异步网络服务器一起使用。准确地说,POST 路由被识别,但处理正文失败。

ESP32 是 WROOM-32。 POST 示例改编自 ESP32 Arduino async HTTP server: Serving HTML

Web 服务器和实际上 ESP32 似乎以我尝试过的所有其他方式工作。虽然我下面的示例没有显示它,但 GET 方法工作得很好。当我尝试处理 POST 请求的主体时出现问题。我在代码中添加了注释“此处的代码未执行”以显示不起作用的内容。显示简单表单“/testform.html”,但提交时,POST 处理程序的 header 部分显示内容类型为“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 正文。在我的测试中,使用您正在使用的表单时我看不到任何参数。

但是:有一个 5 参数版本允许您附加一个主体处理程序。在该函数内部,您可以逐字节读取正文并解析它。这是一个使用 ArduinoJSON 库的示例。

// in your webserver setup function
server.on("/mypage",HTTP_POST, handleMyPage, nullptr, parseMyPageBody);                        


// callback definition
void parseMyPageBody(AsyncWebServerRequest* req, uint8_t* data, size_t len, size_t index, size_t total) {

    DynamicJsonDocument bodyJSON(1024);
    deserializeJson(bodyJSON, data, len);
    String tag1 = bodyJSON["tag1"];
    String tag2 = bodyJSON["tag2"];

    // rest of code
}

https://stackoverflow.com/questions/63842519/

相关文章:

gradle - React Native 项目 Android Gradle Fail (Reac

python-3.x - 如何创建 swagger :response that produces

typescript - 数字和通用参数之间的区别

reactjs - CRA typescript环境导出类型时如何解决解析错误

react-native - 如何在没有 Firebase 的情况下为 React Native A

javascript - select2 on select data-select2-id 属性添

flutter - 如何在 Flutter 中绘制尖三角形边?

tensorflow - Keras SavedModel 与 Tensorflow SavedMo

javascript - 使用逗号自动格式化数字时如何保留预期的光标位置?

python - 使用 tokenizer.encode_plus 时遇到问题