QRestReply — PyQt Documentation v6.9.0 (original) (raw)

PyQt6.QtNetwork.QRestReply

Description

QRestReply is a convenience wrapper for QNetworkReply.

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.

Methods

__init__(QNetworkReply)

Creates a QRestReply and initializes the wrapped QNetworkReply to reply.


error() → NetworkError

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.


errorString() → str

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


hasError() → bool

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.


httpStatus() → int

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().


isHttpStatusSuccess() → bool

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


isSuccess() → bool

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.


networkReply() → QNetworkReply

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


readBody() → QByteArray

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().


readJson(error: QJsonParseError = None) → Optional[QJsonDocument]

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 NoError to distinguish this case from an actual error.

See also

readBody(), readText().


readText() → str

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


swap(QRestReply)

TODO