QRestAccessManager Class

The QRestAccessManager is a convenience wrapper for QNetworkAccessManager . 更多...

头: #include <QRestAccessManager>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network
Since: Qt 6.7
继承: QObject
状态: Preliminary

该类在开发且可能改变。

注意: 此类的所有函数 可重入 .

公共函数

QRestAccessManager (QNetworkAccessManager * manager , QObject * parent = nullptr)
virtual ~QRestAccessManager () override
QNetworkReply * deleteResource (const QNetworkRequest & request , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * get (const QNetworkRequest & request , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * get (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * get (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * get (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * head (const QNetworkRequest & request , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkAccessManager * networkAccessManager () const
QNetworkReply * patch (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * patch (const QNetworkRequest & request , const QVariantMap & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * patch (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * patch (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * post (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * post (const QNetworkRequest & request , const QVariantMap & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * post (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * post (const QNetworkRequest & request , QHttpMultiPart * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * post (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * put (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * put (const QNetworkRequest & request , const QVariantMap & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * put (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * put (const QNetworkRequest & request , QHttpMultiPart * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * put (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & method , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & method , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & method , QHttpMultiPart * data , const QRestAccessManager::ContextTypeForFunctor<Functor> * context , Functor && callback )

详细描述

QRestAccessManager is a convenience wrapper on top of QNetworkAccessManager . It amends datatypes and HTTP methods that are useful for typical RESTful client applications.

The usual Qt networking features are accessible by configuring the wrapped QNetworkAccessManager directly. QRestAccessManager does not take ownership of the wrapped QNetworkAccessManager .

QRestAccessManager and related QRestReply classes can only be used in the thread they live in. See QObject thread affinity 了解更多信息。

Issuing Network Requests and Handling Replies

Network requests are initiated with a function call corresponding to the desired HTTP method, such as get() and post() .

使用信号和槽

函数返回 QNetworkReply * object, whose signals can be used to follow up on the completion of the request in a traditional Qt-signals-and-slots way.

Here's an example of how you could send a GET request and handle the response:

QNetworkReply *reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, this, [reply]() {
    // The reply may be wrapped in the finish handler:
    QRestReply restReply(reply);
    if (restReply.isSuccess())
        // ...
});
					

Using Callbacks and Context Objects

The functions also take a context object of QObject (subclass) type and a callback function as parameters. The callback takes one QRestReply & as a parameter. The callback can be any callable, including a pointer-to-member-function.

These callbacks are invoked when the reply has finished processing (also in the case the processing finished due to an error).

The context object can be nullptr , although, generally speaking, this is discouraged. Using a valid context object ensures that if the context object is destroyed during request processing, the callback will not be called. Stray callbacks which access a destroyed context is a source of application misbehavior.

Here's an example of how you could send a GET request and check the response:

// With lambda
manager->get(request, this, [this](QRestReply &reply) {
    if (reply.isSuccess()) {
        // ...
    }
});
// With member function
manager->get(request, this, &MyClass::handleFinished);
					

Many of the functions take in data for sending to a server. The data is supplied as the second parameter after the request.

Here's an example of how you could send a POST request and check the response:

QJsonDocument myJson;
// ...
manager->post(request, myJson, this, [this](QRestReply &reply) {
    if (!reply.isSuccess()) {
        // ...
    }
    if (std::optional json = reply.readJson()) {
        // use *json
    }
});
					

提供的 QRestReply & is valid only while the callback executes. If you need it for longer, you can either move it to another QRestReply , or construct a new one and initialize it with the QNetworkReply (见 QRestReply::networkReply ()).

Supported data types

The following table summarizes the methods and the supported data types. X means support.

数据类型 get() post() put() head() patch() deleteResource() sendCustomRequest()
No data X - - X - X -
QByteArray X X X - X - X
QJsonDocument *) X X X - X - -
QVariantMap **) - X X - X - -
QHttpMultiPart - X X - - - X
QIODevice X X X - X - X

*) QJsonDocument is sent in QJsonDocument::Compact format, and the Content-Type header is set to application/json Content-Type header was not set

**) QVariantMap is converted to and treated as a QJsonObject

另请参阅 QRestReply , QNetworkRequestFactory ,和 QNetworkAccessManager .

成员函数文档编制

[explicit] QRestAccessManager:: QRestAccessManager ( QNetworkAccessManager * manager , QObject * parent = nullptr)

Constructs a QRestAccessManager object and sets parent as the parent object, and manager as the underlying QNetworkAccessManager which is used for communication.

另请参阅 networkAccessManager ().

[override virtual noexcept] QRestAccessManager:: ~QRestAccessManager ()

销毁 QRestAccessManager 对象。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: deleteResource (const QNetworkRequest & request , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

Issues an HTTP DELETE 基于 request .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->deleteResource(request, this, [this](QRestReply &reply) {
    if (reply.isSuccess())
        // ...
});
					

Alternatively the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

deleteResource() request does not support providing data.

另请参阅 QRestReply .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: get (const QNetworkRequest & request , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

Issues an HTTP GET 基于 request .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->get(request, this, [this](QRestReply &reply) {
    if (!reply.isSuccess())
        // handle error
    if (std::optional json = reply.readJson())
        // use *json
});
					

Alternatively the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

另请参阅 QRestReply .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: get (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

Issues an HTTP GET 基于 request and provided data .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->get(request, myData, this, [this](QRestReply &reply) {
    if (reply.isSuccess())
        // ...
});
					

Alternatively the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

另请参阅 QRestReply .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: get (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: get (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

Issues an HTTP HEAD 基于 request .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->head(request, this, [this](QRestReply &reply) {
    if (reply.isSuccess())
        // ...
});
					

Alternatively the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

head() request does not support providing data.

另请参阅 QRestReply .

QNetworkAccessManager *QRestAccessManager:: networkAccessManager () const

Returns the underlying QNetworkAccessManager 实例。

另请参阅 QNetworkAccessManager .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: patch (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

Issues an HTTP PATCH 基于 request .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->patch(request, myData, this, [this](QRestReply &reply) {
    if (reply.isSuccess())
        // ...
});
					

Alternatively the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

The patch() method always requires data parameter. The following data types are supported:

*) Sent in QJsonDocument::Compact format, and the Content-Type header is set to application/json Content-Type header was not set **) QVariantMap is converted to and treated as a QJsonObject

另请参阅 QRestReply .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: patch (const QNetworkRequest & request , const QVariantMap & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: patch (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: patch (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: post (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

Issues an HTTP POST 基于 request .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->post(request, myData, this, [this](QRestReply &reply) {
    if (reply.isSuccess())
        // ...
});
					

Alternatively, the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

The post() method always requires data parameter. The following data types are supported:

*) Sent in QJsonDocument::Compact format, and the Content-Type header is set to application/json Content-Type header was not set **) QVariantMap is converted to and treated as a QJsonObject

另请参阅 QRestReply .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: post (const QNetworkRequest & request , const QVariantMap & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: post (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: post (const QNetworkRequest & request , QHttpMultiPart * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: post (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: put (const QNetworkRequest & request , const QJsonDocument & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

Issues an HTTP PUT 基于 request .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->put(request, myData, this, [this](QRestReply &reply) {
    if (reply.isSuccess())
        // ...
});
					

Alternatively the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

The put() method always requires data parameter. The following data types are supported:

*) Sent in QJsonDocument::Compact format, and the Content-Type header is set to application/json Content-Type header was not set **) QVariantMap is converted to and treated as a QJsonObject

另请参阅 QRestReply .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: put (const QNetworkRequest & request , const QVariantMap & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: put (const QNetworkRequest & request , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: put (const QNetworkRequest & request , QHttpMultiPart * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: put (const QNetworkRequest & request , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & method , const QByteArray & data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

Issues request based HTTP request with custom method and the provided data .

可选 callback and context object can be provided for handling the request completion as illustrated below:

manager->sendCustomRequest(request, "MYMETHOD",  myData,  this, [this](QRestReply &reply) {
    if (reply.isSuccess())
        // ...
});
					

Alternatively the signals of the returned QNetworkReply * object can be used. For further information see Issuing Network Requests and Handling Replies .

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & method , QIODevice * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。

template <typename Functor, QRestAccessManager::if_compatible_callback<Functor> > QNetworkReply *QRestAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & method , QHttpMultiPart * data , const QRestAccessManager::ContextTypeForFunctor < Functor > * context , Functor && callback )

这是重载函数。