QRestReply Class

QRestReply is a convenience wrapper for QNetworkReply . 更多...

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

该类在开发且可能改变。

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

公共函数

QRestReply (QNetworkReply * reply )
QRestReply (QRestReply && other )
~QRestReply ()
QNetworkReply::NetworkError error () const
QString errorString () const
bool hasError () const
int httpStatus () const
bool isHttpStatusSuccess () const
bool isSuccess () const
QNetworkReply * networkReply () const
QByteArray readBody ()
std::optional<QJsonDocument> readJson (QJsonParseError * error = nullptr)
QString readText ()
QRestReply & operator= (QRestReply && other )
QDebug operator<< (QDebug debug , const QRestReply & reply )

详细描述

QRestReply wraps a QNetworkReply and provides convenience methods for data and status handling. The methods provide convenience for typical RESTful client applications.

QRestReply doesn't take ownership of the wrapped QNetworkReply , and the lifetime and ownership of the reply is as defined by QNetworkAccessManager 文档编制。

QRestReply object is not copyable, but is movable.

另请参阅 QRestAccessManager , QNetworkReply , QNetworkAccessManager ,和 QNetworkAccessManager::setAutoDeleteReplies ().

成员函数文档编制

[explicit] QRestReply:: QRestReply ( QNetworkReply * reply )

Creates a QRestReply and initializes the wrapped QNetworkReply to reply .

[noexcept] QRestReply:: QRestReply ( QRestReply && other )

Move-constructs the reply from other .

注意: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

[noexcept] QRestReply:: ~QRestReply ()

销毁此 QRestReply 对象。

QNetworkReply::NetworkError QRestReply:: error () const

Returns the last error, if any. The errors include errors such as network and protocol errors, but exclude cases when the server successfully responded with an HTTP status.

另请参阅 httpStatus (), isSuccess (), hasError (),和 errorString ().

QString QRestReply:: errorString () const

Returns a human-readable description of the last network error.

另请参阅 httpStatus (), isSuccess (), hasError (),和 error ().

bool QRestReply:: hasError () const

Returns whether an error has occurred. This includes errors such as network and protocol errors, but excludes cases where the server successfully responded with an HTTP error status (for example 500 Internal Server Error ). Use httpStatus () 或 isHttpStatusSuccess () to get the HTTP status information.

另请参阅 httpStatus (), isSuccess (), error (),和 errorString ().

int QRestReply:: httpStatus () const

Returns the HTTP status received in the server response. The value is 0 if not available (the status line has not been received, yet).

注意: The HTTP status is reported as indicated by the received HTTP response. An error () may occur after receiving the status, for instance due to network disconnection while receiving a long response. These potential subsequent errors are not represented by the reported HTTP status.

另请参阅 isSuccess (), hasError (),和 error ().

bool QRestReply:: isHttpStatusSuccess () const

Returns whether the HTTP status is between 200..299.

另请参阅 isSuccess (), httpStatus (), hasError (),和 error ().

bool QRestReply:: isSuccess () const

Returns whether the HTTP status is between 200..299 and no further errors have occurred while receiving the response (for example, abrupt disconnection while receiving the body data). This function is a convenient way to check whether the response is considered successful.

另请参阅 httpStatus (), hasError (),和 error ().

QNetworkReply *QRestReply:: networkReply () const

Returns a pointer to the underlying QNetworkReply wrapped by this object.

QByteArray QRestReply:: readBody ()

Returns the received data as a QByteArray .

Calling this function consumes the data received so far, and any further calls to get response data will return empty until further data has been received.

另请参阅 readJson (), readText (), QNetworkReply::bytesAvailable (),和 QNetworkReply::readyRead ().

std::optional < QJsonDocument > QRestReply:: readJson ( QJsonParseError * error = nullptr)

Returns the received data as a QJsonDocument .

The returned value is wrapped in std::optional . If the conversion from the received data fails (empty data or JSON parsing error), std::nullopt is returned, and error is filled with details.

Calling this function consumes the received data, and any further calls to get response data will return empty.

此函数返回 std::nullopt and will not consume any data if the reply is not finished. If error is passed, it will be set to QJsonParseError::NoError to distinguish this case from an actual error.

另请参阅 readBody () 和 readText ().

QString QRestReply:: readText ()

Returns the received data as a QString .

The received data is decoded into a QString (UTF-16). If available, the decoding uses the Content-Type header's charset parameter to determine the source encoding. If the encoding information is not available or not supported by QStringConverter , UTF-8 is used by default.

Calling this function consumes the data received so far. Returns a default constructed value if no new data is available, or if the decoding is not supported by QStringConverter , or if the decoding has errors (for example invalid characters).

另请参阅 readJson (), readBody (),和 QNetworkReply::readyRead ().

[noexcept] QRestReply &QRestReply:: operator= ( QRestReply && other )

移动赋值 other and returns a reference to this reply.

注意: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

相关非成员

QDebug operator<< ( QDebug debug , const QRestReply & reply )

写入 reply debug object for debugging purposes.

另请参阅 调试技术 .