hoeken / PsychicHttp

Simple + Robust HTTP/S server with websockets for ESP32 based on ESP-IDF http server.
MIT License
131 stars 27 forks source link

Use .serveStatic and .on for same URI #195

Open MicDG opened 1 month ago

MicDG commented 1 month ago

Hello, doing something like this:

httpServer.on("/index.html", HTTP_POST, [](PsychicRequest *request) {
    // Do sth
});
httpServer.serveStatic("/index.html", LittleFS, "/index.html");

causes the server to return an error like "httpd_uri: Method '1' not allowed for URI" when a GET request is made. However doing this works:

httpServer.on("/index.html", HTTP_POST, [](PsychicRequest *request) {
    // Do sth
});
httpServer.on("/index.html", HTTP_GET, [](PsychicRequest *request) {
    // Return file
});

I guess this is due to the fact that .serveStatic is an handler and doesn't register any endpoint. Is there a way around this, beyond using two endpoints (it's just for brevity I guess, not having to reimplement a function to serve the file).

MicDG commented 1 month ago

Sorry, just saw this can be used:


String filename = "/path/to/file";
PsychicFileResponse response(request, LittleFS, filename);

return response.send();

However, maybe this could be added in the ReadMe notes just for clarity.

hitecSmartHome commented 1 month ago

After IDF 5.3 there will be HTTP_ANY support

hoeken commented 1 month ago

Actually in psychic 2.0 we add HTTP_ANY support, and with 5.3 it's much cleaner internally. :)

On Wed, Oct 30, 2024, 04:32 HITEC @.***> wrote:

After IDF 5.3 there will be HTTP_ANY support

— Reply to this email directly, view it on GitHub https://github.com/hoeken/PsychicHttp/issues/195#issuecomment-2446325753, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEHSQIWHHFLCX3VYBKC6TZ6CRTBAVCNFSM6AAAAABQOPO2VGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINBWGMZDKNZVGM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

hitecSmartHome commented 1 month ago

Actually in psychic 2.0 we add HTTP_ANY support, and with 5.3 it's much cleaner internally. :) On Wed, Oct 30, 2024, 04:32 HITEC @.> wrote: After IDF 5.3 there will be HTTP_ANY support — Reply to this email directly, view it on GitHub <#195 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEHSQIWHHFLCX3VYBKC6TZ6CRTBAVCNFSM6AAAAABQOPO2VGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINBWGMZDKNZVGM . You are receiving this because you are subscribed to this thread.Message ID: @.>

Does that mean that the v2 is already using your planned approach with HTTP_ANY and the library's own list of handlers?

mathieucarbou commented 1 month ago

@hitecSmartHome yes!

hitecSmartHome commented 1 month ago

Wow. I must try it