esp8266 / esp8266/Arduino

Using serveStatic() to serve custom page for ESP8266HTTPUpdateServer does not work

Open
#8,539 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

Hi folks,
I'm trying to serve my own customized upload page for ESP8266HTTPUpdateServer ( https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266HTTPUpdateServer).

But it seems, that the GET Request /update does not hit

```
httpServer.serveStatic("/update", LittleFS, "/update.html");
```

because, the default upload page from ESP8266HTTPUpdateServer is sent back to the browser instead of my custom page.

May be it's because serverStatic() does not check the HTTP method of the incoming request? For ESP8266HTTPUpdateServer you have to distinct between GET and POST requests.

My workaround (based on https://www.mischianti.org/2021/11/11/esp8266-ota-update-with-web-browser-custom-web-interface-3/) looks like this. Actually it's rebuilding the serverStatic() method, but I can check whether it's a GET or POST request.

```
bool sendFile(String path, String dataType) {
PRINTLN("Requested page -> ", path);

if (LittleFS.exists(path)) {
File dataFile = LittleFS.open(path, "r");
if (!dataFile) {
httpServer.send(404, F(MIMETYPE_TEXT), F("404: Not Found"));
return false;
}
if (httpServer.streamFile(dataFile, dataType) != dataFile.size()) {
PRINTSLN("Sent less data than expected!");
} else {
PRINTSLN("Page served!");
}

dataFile.close();
} else {
httpServer.send(404, F(MIMETYPE_TEXT), F("404: Not Found"));
return false;
}

return true;
}

void handleUpdate() {
sendFile("/updatefw.html", F(MIMETYPE_HTML));
}

httpServer.on("/myupdate", HTTP_GET, handleUpdate);
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.