QRestReply Class

QRestReply is a convenience wrapper for QNetworkReply. More...

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

Note: All functions in this class are reentrant.

Public Functions

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)

Detailed Description

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 documentation.

QRestReply object is not copyable, but is movable.

See also QRestAccessManager, QNetworkReply, QNetworkAccessManager, and QNetworkAccessManager::setAutoDeleteReplies().

Member Function Documentation

[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.

Note: 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()

Destroys this QRestReply object.

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.

See also httpStatus(), isSuccess(), hasError(), and errorString().

QString QRestReply::errorString() const

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

See also httpStatus(), isSuccess(), hasError(), and 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() or isHttpStatusSuccess() to get the HTTP status information.

See also httpStatus(), isSuccess(), error(), and 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).

Note: 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.

See also isSuccess(), hasError(), and error().

bool QRestReply::isHttpStatusSuccess() const

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

See also isSuccess(), httpStatus(), hasError(), and 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.

See also httpStatus(), hasError(), and 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.

See also readJson(), readText(), QNetworkReply::bytesAvailable(), and 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.

This function returns 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.

See also readBody() and 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).

See also readJson(), readBody(), and QNetworkReply::readyRead().

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

Move-assigns other and returns a reference to this reply.

Note: 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.

Related Non-Members

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

Writes the reply into the debug object for debugging purposes.

See also Debugging Techniques.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.