me-no-dev / ESPAsyncWebServer

Async Web Server for ESP8266 and ESP32
3.7k stars 1.21k forks source link

How to get the url path? #1374

Open AC-Mike opened 8 months ago

AC-Mike commented 8 months ago

I have several pages that call the same /getValue url. For example, 192.168.0.12/path1/getValue and 192.168.0.12/path2/getValue.

in server.on("/getValue",...) request->url() only returns the /getValue of the url. How do I determine which path the /getValue was from? How do you determine the context for /getvalue so I can serve up the correct value?

AC-Mike commented 8 months ago

I discovered this is not how it works. But, I did discover adding regex to the project build_flags = -DASYNCWEBSERVER_REGEX and using this to initialize the callback handler:

    server->on("^\\/(?:.+\\/)?getPulsesReceived$", HTTP_GET, [](AsyncWebServerRequest *request)
               { request->send(200, "text/plain", String(_pulses_received)); });

If I do this before initializing the parent path handlers then it will respond to the urls /foo/getPulsesReceived and /bar/getPulsesReceived and /foo and /bar are handled by them selves.

I hope this helps others.