QHttpServerRouter Class

Provides functions to bind a path to a ViewHandler. More...

Header: #include <QHttpServerRouter>
CMake: find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
qmake: QT += httpserver
Since: Qt 6.4

Detailed Description

QHttpServerRouter is a class to distribute HTTP requests to their respective handlers with a rule based system.

You can register new QHttpServerRouterRules, that represent a request path and the respective handler. Variable parts in the route can be specified with placeholder in the request path. The handler gets the placeholders value as a QRegularExpressionMatch. The arguments can be of any type for which a converter is available. The handler creation can be simplified with QHttpServerRouterRule::bindCaptured(). A QHttpServerRouter instance must not be modifed by its rules.

Note: This is a low-level routing API for an HTTP server.

Minimal example:

 auto pageView = [] (const quint64 page) {
     qDebug() << "page" << page;
 };
 using ViewHandler = decltype(pageView);

 QHttpServerRouter router;

 // register callback pageView on request "/page/<number>"
 // for example: "/page/10", "/page/15"
 router.addRule<ViewHandler>(
     new QHttpServerRouterRule("/page/", [=] (QRegularExpressionMatch &match,
                                              const QHttpServerRequest &,
                                              QHttpServerResponder &&) {
     auto boundView = QHttpServerRouterRule::bindCaptured(pageView, match);

     // it calls pageView
     boundView();
 }));