You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32 CAM向本地XAMPP服务器发送SD卡文件时返回HTTP 301错误求助

ESP32 CAM向本地XAMPP服务器发送SD卡文件时返回HTTP 301错误求助

Hey there! Let's break down why you're getting that 301 "Moved Permanently" error and fix it step by step.

First, a quick recap: the 301 response means your XAMPP server is telling your ESP32 that the URL you're requesting (/ProjectFolder) isn't the right one—it wants you to use /ProjectFolder/ (with a trailing slash) instead. That's why your phone works when you use the full path with the slash, but the ESP doesn't.

Let's go through the fixes:

1. Fix the Requested URL

The first thing to do is update your path variable to include the trailing slash, so your ESP sends the request to the exact URL the server expects. Change:

const String path = "/ProjectFolder";

to:

const String path = "/ProjectFolder/";

This ensures your http.begin() call uses the correct URL right from the start, avoiding the redirect altogether.

2. Remove the Unnecessary Location Header

In your code, you're adding a Location header after sending the POST request—this is a mistake. The Location header is something the server sends to tell clients where to redirect, not something the client should send. Delete this line entirely:

http.addHeader("Location", "192.168.0.106:8080/ProjectFolder/");

It's not helping and might even be causing confusion for the server.

3. Optional: Enable Automatic Redirects (Just in Case)

If you ever forget the trailing slash or want your ESP to handle redirects automatically, you can enable HTTPClient's redirect following feature. Add this line right after initializing HTTPClient http;:

http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);

This will make the ESP automatically follow the server's 301 redirect to the correct URL without you having to adjust the path manually.

Modified Full Code

Here's your complete code with all the fixes applied:

#include <WiFi.h>
#include <HTTPClient.h>
#include "SD_MMC.h"

const char* ssid = "name";
const char* password = "pass";
const char* server = "192.168.0.106";
const int port = 8080;
// Fixed path with trailing slash
const String path = "/ProjectFolder/";
const char* filename = "/data.txt"; // replace with your text file name on SD card

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

  // Connect to Wi-Fi network
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");

  // Initialize SD card
  if (!SD_MMC.begin()) {
    Serial.println("SD card initialization failed");
    return;
  }
  Serial.println("SD card initialized");
}

void loop() {
  // Wait for a few seconds
  delay(5000);

  // Check if connected to WiFi
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi connection lost");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(1000);
      Serial.print(".");
    }
    Serial.println("Reconnected to WiFi");
  }

  // Make HTTP request to server
  HTTPClient http;
  // Optional: Enable automatic redirects
  http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  http.begin("http://" + String(server) + ":" + String(port) + path);

  // Open the text file from SD card
  File file = SD_MMC.open(filename);
  if (!file) {
    Serial.println("Failed to open file");
    return;
  }

  // Read the text file into a String object
  String data = "";
  while (file.available()) {
    data += char(file.read());
  }
  file.close();

  // Send the text file contents to the server
  http.addHeader("Content-Type", "text/plain");
  int httpCode = http.POST(data);
  
  // Removed the unnecessary Location header here
  String response = http.getString();
  http.end();

  // Check HTTP response
  if (httpCode > 0) {
    Serial.printf("HTTP response code: %d\n", httpCode);
    Serial.println("HTTP response body: " + response);
  } else {
    Serial.println("HTTP request failed");
  }
}

Quick Check

After making these changes, test your ESP again. You should stop seeing the 301 error and get a successful response (like 200 OK) from your XAMPP server. Also, double-check that your ProjectFolder directory on XAMPP has proper write permissions if you're expecting the server to save the uploaded file—since you mentioned the folder is empty, make sure Apache can write to it.

备注:内容来源于stack exchange,提问作者theboss12k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 10:44:11