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 rule-based system for routing HTTP requests to their appropriate handlers. You can add QHttpServerRouterRule instances, which define a request path and its corresponding handler.
Variable parts in the route can be specified with placeholders ("<arg>") in the request path, but it is not needed at the end. The handler receives the matched values 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().
Note: 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(); }));