PySide6.QtHttpServer.QHttpServerRouter¶
- class QHttpServerRouter¶
Provides functions to bind a URL to a
ViewHandler
. More…Synopsis¶
Methods¶
def
__init__()
def
addConverter()
def
converters()
def
handleRequest()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
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 aconverter
is available. The handler creation can be simplified withbindCaptured
. AQHttpServerRouter
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(); }));
- __init__(server)¶
- Parameters:
server –
QAbstractHttpServer
Creates a
QHttpServerRouter
object with default converters.See also
Adds a new converter for
metaType
that can be parsed withregexp
. Having a converter for ametaType
enables to use this type in a path pattern of aQHttpServerRouterRule
. The regular expression is used to parse parameters of typemetaType
from the path pattern.If there is already a converter of type
metaType
, that converter’s regexp is replaced withregexp
.See also
- clearConverters()¶
Removes all converters.
- converters()¶
- Return type:
Dictionary with keys of type .QMetaType and values of type QString.
- handleRequest(request, responder)¶
- Parameters:
request –
QHttpServerRequest
responder –
QHttpServerResponder
- Return type:
bool
Handles each new
request
for the HTTP server usingresponder
.Iterates through the list of rules to find the first that matches, then executes this rule, returning
true
. Returnsfalse
if no rule matches the request.Removes the converter for type
metaType
.See also