ESPAsyncWebServer icon indicating copy to clipboard operation
ESPAsyncWebServer copied to clipboard

server.on routing

Open UserNulled opened this issue 2 years ago • 1 comments

if I make "/system" page and "/system/test" page only "/system" page is working when using ESP8266Webserver library it was working with no problem.

server->on("/system", HTTP_ANY, [](AsyncWebServerRequest* request) {
  request->send(200, "text/plain", "system");
 });

server->on("/system/test", HTTP_ANY, [](AsyncWebServerRequest* request) {
   request->send(200, "text/plain", "system test");
 });

is it normal behavior using this library?

UserNulled avatar Jul 12 '23 13:07 UserNulled

I faced the same issue. However, I managed the following workaround:

  • Convert all URIs into regex
  • Enable compiler flag for regex support

To implement it:

// define a function to convert plaintext URIs into regex
String toRegex(const String &uri)
{
	String result = url;
	result.replace("/", "\\/");
	return "^" + result + "(\\?.*)?$";
}

// use the toRegex function when routing
server->on(toRegex("/system").c_str(), HTTP_ANY, [](AsyncWebServerRequest* request) {
	request->send(200, "text/plain", "system");
 });

server->on(toRegex("/system/test").c_str(), HTTP_ANY, [](AsyncWebServerRequest* request) {
	request->send(200, "text/plain", "system test");
 });

Remember to add the compiler flag for regex support: -DASYNCWEBSERVER_REGEX

Zhu-jiatong avatar Nov 10 '23 21:11 Zhu-jiatong

@me-no-dev Is there any reason that the AsyncWebServer is not using the Uri class that comes with ESP8266WebServer? It made URI Definition much easier and cleaner (using UriBraces, UriRegex and such).

I might be able to create a pull request.

IPdotSetAF avatar Feb 02 '24 11:02 IPdotSetAF