PySide6.QtHttpServer.QHttpServerRouter

class QHttpServerRouter

Provides functions to bind a path to a ViewHandler.

Details

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 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();
}));

Synopsis

Methods

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

__init__(server)
Parameters:

serverQAbstractHttpServer

Creates a QHttpServerRouter object with default converters.

See also

converters

addConverter(metaType, regexp)
Parameters:

Adds a new converter for metaType that can be parsed with regexp. Having a converter for a metaType enables to use this type in a path pattern of a QHttpServerRouterRule . The regular expression is used to parse parameters of type metaType from the path pattern.

If there is already a converter of type metaType, that converter’s regexp is replaced with regexp.

clearConverters()

Removes all converters.

Note

clearConverters() does not set up default converters.

converters()
Return type:

Dictionary with keys of type .QMetaType and values of type QString.

handleRequest(request, responder)
Parameters:
Return type:

bool

Handles each new request for the HTTP server using responder.

Iterates through the list of rules to find the first that matches, then executes this rule, returning true. Returns false if no rule matches the request.

removeConverter(metaType)
Parameters:

metaTypeQMetaType

Removes the converter for type metaType.

See also

addConverter