Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QRestReply Class Reference

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

#include <qrestreply.h>

+ Collaboration diagram for QRestReply:

Public Member Functions

Q_NETWORK_EXPORT QRestReply (QNetworkReply *reply)
 Creates a QRestReply and initializes the wrapped QNetworkReply to reply.
 
Q_NETWORK_EXPORT ~QRestReply ()
 Destroys this QRestReply object.
 
 QRestReply (QRestReply &&other) noexcept
 Move-constructs the reply from other.
 
void swap (QRestReply &other) noexcept
 
Q_NETWORK_EXPORT QNetworkReplynetworkReply () const
 Returns a pointer to the underlying QNetworkReply wrapped by this object.
 
Q_NETWORK_EXPORT std::optional< QJsonDocumentreadJson (QJsonParseError *error=nullptr)
 Returns the received data as a QJsonDocument.
 
Q_NETWORK_EXPORT QByteArray readBody ()
 Returns the received data as a QByteArray.
 
Q_NETWORK_EXPORT QString readText ()
 Returns the received data as a QString.
 
bool 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).
 
Q_NETWORK_EXPORT int httpStatus () const
 Returns the HTTP status received in the server response.
 
Q_NETWORK_EXPORT bool isHttpStatusSuccess () const
 Returns whether the HTTP status is between 200..299.
 
Q_NETWORK_EXPORT bool hasError () const
 Returns whether an error has occurred.
 
Q_NETWORK_EXPORT QNetworkReply::NetworkError error () const
 Returns the last error, if any.
 
Q_NETWORK_EXPORT QString errorString () const
 Returns a human-readable description of the last network error.
 

Friends

Q_NETWORK_EXPORT QDebug operator<< (QDebug debug, const QRestReply &reply)
 Writes the reply into the debug object for debugging purposes.
 

Detailed Description

QRestReply is a convenience wrapper for QNetworkReply.

Since
6.7

\reentrant

\inmodule QtNetwork

\preliminary

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, QNetworkAccessManager::setAutoDeleteReplies()

Definition at line 23 of file qrestreply.h.

Constructor & Destructor Documentation

◆ QRestReply() [1/2]

QRestReply::QRestReply ( QNetworkReply * reply)
explicit

Creates a QRestReply and initializes the wrapped QNetworkReply to reply.

Definition at line 51 of file qrestreply.cpp.

References qCWarning.

◆ ~QRestReply()

QRestReply::~QRestReply ( )

Destroys this QRestReply object.

Definition at line 61 of file qrestreply.cpp.

◆ QRestReply() [2/2]

QRestReply::QRestReply ( QRestReply && other)
inlinenoexcept

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.

Definition at line 29 of file qrestreply.h.

Member Function Documentation

◆ error()

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(), errorString()

Definition at line 266 of file qrestreply.cpp.

References QNetworkReply::error(), hasError(), and QNetworkReply::NoError.

Referenced by readJson().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ errorString()

QString QRestReply::errorString ( ) const

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

See also
httpStatus(), isSuccess(), hasError(), error()

Definition at line 278 of file qrestreply.cpp.

References QIODevice::errorString(), and hasError().

+ Here is the call graph for this function:

◆ hasError()

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 \l httpStatus() or \l isHttpStatusSuccess() to get the HTTP status information.

See also
httpStatus(), isSuccess(), error(), errorString()

Definition at line 245 of file qrestreply.cpp.

References QNetworkReply::error(), httpStatus(), QNetworkReply::NoError, and QNetworkReply::RemoteHostClosedError.

Referenced by error(), and errorString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ httpStatus()

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(), error()

Definition at line 209 of file qrestreply.cpp.

References QNetworkReply::attribute(), QNetworkRequest::HttpStatusCodeAttribute, and QVariant::toInt().

Referenced by hasError(), and isHttpStatusSuccess().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isHttpStatusSuccess()

bool QRestReply::isHttpStatusSuccess ( ) const

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

See also
isSuccess(), httpStatus(), hasError(), error()

Definition at line 230 of file qrestreply.cpp.

References httpStatus().

+ Here is the call graph for this function:

◆ isSuccess()

bool QRestReply::isSuccess ( ) const
inline

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(), error()

Definition at line 47 of file qrestreply.h.

◆ networkReply()

QNetworkReply * QRestReply::networkReply ( ) const

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

Definition at line 89 of file qrestreply.cpp.

◆ readBody()

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(), QNetworkReply::readyRead()

Definition at line 145 of file qrestreply.cpp.

References QIODevice::readAll().

+ Here is the call graph for this function:

◆ readJson()

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(), readText()

Definition at line 111 of file qrestreply.cpp.

References QJsonParseError::error, error(), QJsonDocument::fromJson(), QNetworkReply::isFinished(), QJsonParseError::NoError, qCWarning, and QIODevice::readAll().

+ Here is the call graph for this function:

◆ 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 \l 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 \l QStringConverter, or if the decoding has errors (for example invalid characters).

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

Definition at line 165 of file qrestreply.cpp.

References QByteArray::constData(), QRestReplyPrivate::contentCharset(), QRestReplyPrivate::decoder, qCWarning, and QIODevice::readAll().

+ Here is the call graph for this function:

◆ swap()

void QRestReply::swap ( QRestReply & other)
inlinenoexcept

Definition at line 35 of file qrestreply.h.

References d, other(), and qt_ptr_swap().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ operator<<

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

Writes the reply into the debug object for debugging purposes.

See also
{Debugging Techniques}

Definition at line 320 of file qrestreply.cpp.


The documentation for this class was generated from the following files: