- Qt 6.10
- Qt Network
- C++ Classes
- QNetworkReply
- Qt 6.10.2 Reference Documentation

# QNetworkReply Class

The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager. More...

Header: | `#include <QNetworkReply>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Network)` `target_link_libraries(mytarget PRIVATE Qt6::Network)`
qmake: | `QT += network`
Inherits: | QIODevice

- List of all members, including inherited members
- QNetworkReply is part of Network Programming API.

Note: All functions in this class are reentrant.

## Public Types

enum | NetworkError { NoError, ConnectionRefusedError, RemoteHostClosedError, HostNotFoundError, TimeoutError, …, UnknownServerError }
| RawHeaderPair

## Public Functions

virtual | ~QNetworkReply()
QVariant | attribute(QNetworkRequest::Attribute code) const
QNetworkReply::NetworkError | error() const
bool | hasRawHeader(QAnyStringView headerName) const
QVariant | header(QNetworkRequest::KnownHeaders header) const
`(since 6.8)` QHttpHeaders | headers() const
bool | isFinished() const
bool | isRunning() const
QNetworkAccessManager * | manager() const
QNetworkAccessManager::Operation | operation() const
QByteArray | rawHeader(QAnyStringView headerName) const
QList<QByteArray> | rawHeaderList() const
const QList<QNetworkReply::RawHeaderPair> & | rawHeaderPairs() const
qint64 | readBufferSize() const
QNetworkRequest | request() const
virtual void | setReadBufferSize(qint64 size)
QUrl | url() const

## Reimplemented Public Functions

virtual void | close() override
virtual bool | isSequential() const override

## Public Slots

virtual void | abort() = 0
virtual void | ignoreSslErrors()

## Signals

void | downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void | errorOccurred(QNetworkReply::NetworkError code)
void | finished()
void | metaDataChanged()
void | redirectAllowed()
void | redirected(const QUrl &url)
`(since 6.3)` void | requestSent()
`(since 6.3)` void | socketStartedConnecting()
void | uploadProgress(qint64 bytesSent, qint64 bytesTotal)

## Protected Functions

| QNetworkReply(QObject *parent = nullptr)
void | setAttribute(QNetworkRequest::Attribute code, const QVariant &value)
void | setError(QNetworkReply::NetworkError errorCode, const QString &errorString)
void | setFinished(bool finished)
void | setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
`(since 6.8)` void | setHeaders(const QHttpHeaders &newHeaders)
`(since 6.8)` void | setHeaders(QHttpHeaders &&newHeaders)
void | setOperation(QNetworkAccessManager::Operation operation)
void | setRawHeader(const QByteArray &headerName, const QByteArray &value)
void | setRequest(const QNetworkRequest &request)
void | setUrl(const QUrl &url)
`(since 6.8)` void | setWellKnownHeader(QHttpHeaders::WellKnownHeader name, QByteArrayView value)

## Reimplemented Protected Functions

virtual qint64 | writeData(const char *data, qint64 len) override

## Detailed Description

The QNetworkReply class contains the data and metadata related to a request posted with QNetworkAccessManager. Like QNetworkRequest, it contains a URL and headers (both in parsed and raw form), some information about the reply's state and the contents of the reply itself.

QNetworkReply is a sequential-access QIODevice, which means that once data is read from the object, it is no longer kept by the device. It is therefore the application's responsibility to keep this data if it needs to. Whenever more data is received from the network and processed, the readyRead() signal is emitted.

The downloadProgress() signal is also emitted when data is received, but the number of bytes contained in it may not represent the actual bytes received, if any transformation is done to the contents (for example, decompressing and removing the protocol overhead).

Even though QNetworkReply is a QIODevice connected to the contents of the reply, it also emits the uploadProgress() signal, which indicates the progress of the upload for operations that have such content.

Note: Do not delete the object in the slot connected to the errorOccurred() or finished() signal. Use deleteLater().

See also QNetworkRequest and QNetworkAccessManager.

## Member Type Documentation

### enum QNetworkReply::NetworkError

Indicates all possible error conditions found during the processing of the request.

Constant | Value | Description
--- | --- | ---
`QNetworkReply::NoError` | `0` | no error condition.

Note: When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with the QNetworkRequest::RedirectionTargetAttribute attribute.

Constant | Value | Description
--- | --- | ---
`QNetworkReply::ConnectionRefusedError` | `1` | the remote server refused the connection (the server is not accepting requests)
`QNetworkReply::RemoteHostClosedError` | `2` | the remote server closed the connection prematurely, before the entire reply was received and processed
`QNetworkReply::HostNotFoundError` | `3` | the remote host name was not found (invalid hostname)
`QNetworkReply::TimeoutError` | `4` | the connection to the remote server timed out
`QNetworkReply::OperationCanceledError` | `5` | the operation was canceled via calls to abort() or close() before it was finished.
`QNetworkReply::SslHandshakeFailedError` | `6` | the SSL/TLS handshake failed and the encrypted channel could not be established. The sslErrors() signal should have been emitted.
`QNetworkReply::TemporaryNetworkFailureError` | `7` | the connection was broken due to disconnection from the network, however the system has initiated roaming to another access point. The request should be resubmitted and will be processed as soon as the connection is re-established.
`QNetworkReply::NetworkSessionFailedError` | `8` | the connection was broken due to disconnection from the network or failure to start the network.
`QNetworkReply::BackgroundRequestNotAllowedError` | `9` | the background request is not currently allowed due to platform policy.
`QNetworkReply::TooManyRedirectsError` | `10` | while following redirects, the maximum limit was reached. The limit is by default set to 50 or as set by QNetworkRequest::setMaxRedirectsAllowed(). (This value was introduced in 5.6.)
`QNetworkReply::InsecureRedirectError` | `11` | while following redirects, the network access API detected a redirect from a encrypted protocol (https) to an unencrypted one (http). (This value was introduced in 5.6.)
`QNetworkReply::ProxyConnectionRefusedError` | `101` | the connection to the proxy server was refused (the proxy server is not accepting requests)
`QNetworkReply::ProxyConnectionClosedError` | `102` | the proxy server closed the connection prematurely, before the entire reply was received and processed
`QNetworkReply::ProxyNotFoundError` | `103` | the proxy host name was not found (invalid proxy hostname)
`QNetworkReply::ProxyTimeoutError` | `104` | the connection to the proxy timed out or the proxy did not reply in time to the request sent
`QNetworkReply::ProxyAuthenticationRequiredError` | `105` | the proxy requires authentication in order to honour the request but did not accept any credentials offered (if any)
`QNetworkReply::ContentAccessDenied` | `201` | the access to the remote content was denied (similar to HTTP error 403)
`QNetworkReply::ContentOperationNotPermittedError` | `202` | the operation requested on the remote content is not permitted
`QNetworkReply::ContentNotFoundError` | `203` | the remote content was not found at the server (similar to HTTP error 404)
`QNetworkReply::AuthenticationRequiredError` | `204` | the remote server requires authentication to serve the content but the credentials provided were not accepted (if any)
`QNetworkReply::ContentReSendError` | `205` | the request needed to be sent again, but this failed for example because the upload data could not be read a second time.
`QNetworkReply::ContentConflictError` | `206` | the request could not be completed due to a conflict with the current state of the resource.
`QNetworkReply::ContentGoneError` | `207` | the requested resource is no longer available at the server.
`QNetworkReply::InternalServerError` | `401` | the server encountered an unexpected condition which prevented it from fulfilling the request.
`QNetworkReply::OperationNotImplementedError` | `402` | the server does not support the functionality required to fulfill the request.
`QNetworkReply::ServiceUnavailableError` | `403` | the server is unable to handle the request at this time.
`QNetworkReply::ProtocolUnknownError` | `301` | the Network Access API cannot honor the request because the protocol is not known
`QNetworkReply::ProtocolInvalidOperationError` | `302` | the requested operation is invalid for this protocol
`QNetworkReply::UnknownNetworkError` | `99` | an unknown network-related error was detected
`QNetworkReply::UnknownProxyError` | `199` | an unknown proxy-related error was detected
`QNetworkReply::UnknownContentError` | `299` | an unknown error related to the remote content was detected
`QNetworkReply::ProtocolFailure` | `399` | a breakdown in protocol was detected (parsing error, invalid or unexpected responses, etc.)
`QNetworkReply::UnknownServerError` | `499` | an unknown error related to the server response was detected

See also error() and errorOccurred().

### QNetworkReply::RawHeaderPair

RawHeaderPair is a std::pair<QByteArray, QByteArray> where the first QByteArray is the header name and the second is the header.

## Member Function Documentation

### `[explicit protected]` QNetworkReply::QNetworkReply(QObject *parent = nullptr)

Creates a QNetworkReply object with parent parent.

You cannot directly instantiate QNetworkReply objects. Use QNetworkAccessManager functions to do that.

### `[virtual noexcept]` QNetworkReply::~QNetworkReply()

Disposes of this reply and frees any resources associated with it. If any network connections are still open, they will be closed.

See also abort() and close().

### `[pure virtual slot]` void QNetworkReply::abort()

Aborts the operation immediately and closes any network connections still open. Uploads still in progress are also aborted.

The finished() signal will also be emitted.

See also close() and finished().

### QVariant QNetworkReply::attribute(QNetworkRequest::Attribute code) const

Returns the attribute associated with the code code. If the attribute has not been set, it returns an invalid QVariant (type QMetaType::UnknownType).

You can expect the default values listed in QNetworkRequest::Attribute to be applied to the values returned by this function.

See also setAttribute() and QNetworkRequest::Attribute.

### `[override virtual]` void QNetworkReply::close()

Reimplements: QIODevice::close().

Closes this device for reading. Unread data is discarded, but the network resources are not discarded until they are finished. In particular, if any upload is in progress, it will continue until it is done.

The finished() signal is emitted when all operations are over and the network resources are freed.

See also abort() and finished().

### `[signal]` void QNetworkReply::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)

This signal is emitted to indicate the progress of the download part of this network request, if there's any. If there's no download associated with this request, this signal will be emitted once with 0 as the value of both bytesReceived and bytesTotal.

The bytesReceived parameter indicates the number of bytes received, while bytesTotal indicates the total number of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, bytesTotal will be -1.

The download is finished when bytesReceived is equal to bytesTotal. At that time, bytesTotal will not be -1.

Note that the values of both bytesReceived and bytesTotal may be different from size(), the total number of bytes obtained through read() or readAll(), or the value of the header(ContentLengthHeader). The reason for that is that there may be protocol overhead or the data may be compressed during the download.

See also uploadProgress() and bytesAvailable().

### QNetworkReply::NetworkError QNetworkReply::error() const

Returns the error that was found during the processing of this request. If no error was found, returns NoError.

See also setError().

### `[signal]` void QNetworkReply::errorOccurred(QNetworkReply::NetworkError code)

This signal is emitted when the reply detects an error in processing. The finished() signal will probably follow, indicating that the connection is over.

The code parameter contains the code of the error that was detected. Call errorString() to obtain a textual representation of the error condition.

Note: Do not delete the object in the slot connected to this signal. Use deleteLater().

See also error() and errorString().

### `[signal]` void QNetworkReply::finished()

This signal is emitted when the reply has finished processing. After this signal is emitted, there will be no more updates to the reply's data or metadata.

Unless close() or abort() have been called, the reply will still be open for reading, so the data can be retrieved by calls to read() or readAll(). In particular, if no calls to read() were made as a result of readyRead(), a call to readAll() will retrieve the full contents in a QByteArray.

This signal is emitted in tandem with QNetworkAccessManager::finished() where that signal's reply parameter is this object.

Note: Do not delete the object in the slot connected to this signal. Use deleteLater().

You can also use isFinished() to check if a QNetworkReply has finished even before you receive the finished() signal.

See also setFinished(), QNetworkAccessManager::finished(), and isFinished().

### bool QNetworkReply::hasRawHeader(QAnyStringView headerName) const

Returns `true` if the raw header of name headerName was sent by the remote server.

Note: In Qt versions prior to 6.7, this function took QByteArray only.

See also rawHeader().

### QVariant QNetworkReply::header(QNetworkRequest::KnownHeaders header) const

Returns the value of the known header header, if that header was sent by the remote server. If the header was not sent, returns an invalid QVariant.

See also rawHeader(), setHeader(), and QNetworkRequest::header().

### `[since 6.8]` QHttpHeaders QNetworkReply::headers() const

Returns headers that were sent by the remote server.

This function was introduced in Qt 6.8.

See also setHeaders(), QNetworkRequest::setAttribute(), and QNetworkRequest::Attribute.

### `[virtual slot]` void QNetworkReply::ignoreSslErrors()

If this function is called, SSL errors related to network connection will be ignored, including certificate validation errors.

Warning: Be sure to always let the user inspect the errors reported by the sslErrors() signal, and only call this method upon confirmation from the user that proceeding is ok. If there are unexpected errors, the reply should be aborted. Calling this method without inspecting the actual errors will most likely pose a security risk for your application. Use it with great care!

This function can be called from the slot connected to the sslErrors() signal, which indicates which errors were found.

Note: If HTTP Strict Transport Security is enabled for QNetworkAccessManager, this function has no effect.

See also sslConfiguration(), sslErrors(), and QSslSocket::ignoreSslErrors().

### bool QNetworkReply::isFinished() const

Returns `true` when the reply has finished or was aborted.

See also isRunning().

### bool QNetworkReply::isRunning() const

Returns `true` when the request is still processing and the reply has neither finished nor been aborted yet.

See also isFinished().

### `[override virtual]` bool QNetworkReply::isSequential() const

Reimplements: QIODevice::isSequential() const.

### QNetworkAccessManager *QNetworkReply::manager() const

Returns the QNetworkAccessManager that was used to create this QNetworkReply object. Initially, it is also the parent object.

### `[signal]` void QNetworkReply::metaDataChanged()

This signal is emitted whenever the metadata in this reply changes. metadata is any information that is not the content (data) itself, including the network headers. In the majority of cases, the metadata will be fully known by the time the first byte of data is received. However, it is possible to receive updates of headers or other metadata during the processing of the data.

See also header(), rawHeaderList(), rawHeader(), and hasRawHeader().

### QNetworkAccessManager::Operation QNetworkReply::operation() const

Returns the operation that was posted for this reply.

See also setOperation().

### QByteArray QNetworkReply::rawHeader(QAnyStringView headerName) const

Returns the raw contents of the header headerName as sent by the remote server. If there is no such header, returns an empty byte array, which may be indistinguishable from an empty header. Use hasRawHeader() to verify if the server sent such header field.

Note: In Qt versions prior to 6.7, this function took QByteArray only.

See also setRawHeader(), hasRawHeader(), and header().

### QList<QByteArray> QNetworkReply::rawHeaderList() const

Returns a list of header fields that were sent by the remote server, in the order that they were sent. Duplicate headers are skipped.

### const QList<QNetworkReply::RawHeaderPair> &QNetworkReply::rawHeaderPairs() const

Returns a list of raw header pairs.

### qint64 QNetworkReply::readBufferSize() const

Returns the size of the read buffer, in bytes.

See also setReadBufferSize().

### `[signal]` void QNetworkReply::redirectAllowed()

When client code handling the redirected() signal has verified the new URL, it emits this signal to allow the redirect to go ahead. This protocol applies to network requests whose redirects policy is set to QNetworkRequest::UserVerifiedRedirectPolicy

See also QNetworkRequest::UserVerifiedRedirectPolicy, QNetworkAccessManager::setRedirectPolicy(), and QNetworkRequest::RedirectPolicyAttribute.

### `[signal]` void QNetworkReply::redirected(const QUrl &url)

This signal is emitted if the QNetworkRequest::ManualRedirectPolicy was not set in the request and the server responded with a 3xx status (specifically 301, 302, 303, 305, 307 or 308 status code) with a valid url in the location header, indicating an HTTP redirect. The url parameter contains the new redirect url as returned by the server in the location header.

See also QNetworkRequest::RedirectPolicy.

### QNetworkRequest QNetworkReply::request() const

Returns the request that was posted for this reply. In particular, note that the URL for the request may be different than that of the reply.

See also QNetworkRequest::url(), url(), and setRequest().

### `[signal, since 6.3]` void QNetworkReply::requestSent()

This signal is emitted 1 or more times when the request was sent. Useful for custom progress or timeout handling.

This function was introduced in Qt 6.3.

See also metaDataChanged() and socketStartedConnecting().

### `[protected]` void QNetworkReply::setAttribute(QNetworkRequest::Attribute code, const QVariant &value)

Sets the attribute code to have value value. If code was previously set, it will be overridden. If value is an invalid QVariant, the attribute will be unset.

See also attribute() and QNetworkRequest::setAttribute().

### `[protected]` void QNetworkReply::setError(QNetworkReply::NetworkError errorCode, const QString &errorString)

Sets the error condition to be errorCode. The human-readable message is set with errorString.

Calling setError() does not emit the errorOccurred(QNetworkReply::NetworkError) signal.

See also error() and errorString().

### `[protected]` void QNetworkReply::setFinished(bool finished)

Sets the reply as finished.

After having this set the reply's data must not change.

See also finished() and isFinished().

### `[protected]` void QNetworkReply::setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)

Sets the known header header to be of value value. The corresponding raw form of the header will be set as well.

See also header(), setRawHeader(), and QNetworkRequest::setHeader().

### `[protected, since 6.8]` void QNetworkReply::setHeaders(const QHttpHeaders &newHeaders)

Sets newHeaders as headers in this network reply, overriding any previously set headers.

If some headers correspond to the known headers, they will be parsed and the corresponding parsed form will also be set.

This function was introduced in Qt 6.8.

See also headers() and QNetworkRequest::KnownHeaders.

### `[protected, since 6.8]` void QNetworkReply::setHeaders(QHttpHeaders &&newHeaders)

This is an overloaded function.

This function was introduced in Qt 6.8.

### `[protected]` void QNetworkReply::setOperation(QNetworkAccessManager::Operation operation)

Sets the associated operation for this object to be operation. This value will be returned by operation().

Note: The operation should be set when this object is created and not changed again.

See also operation() and setRequest().

### `[protected]` void QNetworkReply::setRawHeader(const QByteArray &headerName, const QByteArray &value)

Sets the raw header headerName to be of value value. If headerName was previously set, it is overridden. Multiple HTTP headers of the same name are functionally equivalent to one single header with the values concatenated, separated by commas.

If headerName matches a known header, the value value will be parsed and the corresponding parsed form will also be set.

See also rawHeader(), header(), setHeader(), and QNetworkRequest::setRawHeader().

### `[virtual]` void QNetworkReply::setReadBufferSize(qint64 size)

Sets the size of the read buffer to be size bytes. The read buffer is the buffer that holds data that is being downloaded off the network, before it is read with QIODevice::read(). Setting the buffer size to 0 will make the buffer unlimited in size.

QNetworkReply will try to stop reading from the network once this buffer is full (i.e., bytesAvailable() returns size or more), thus causing the download to throttle down as well. If the buffer is not limited in size, QNetworkReply will try to download as fast as possible from the network.

Unlike QAbstractSocket::setReadBufferSize(), QNetworkReply cannot guarantee precision in the read buffer size. That is, bytesAvailable() can return more than size.

See also readBufferSize().

### `[protected]` void QNetworkReply::setRequest(const QNetworkRequest &request)

Sets the associated request for this object to be request. This value will be returned by request().

Note: The request should be set when this object is created and not changed again.

See also request() and setOperation().

### `[protected]` void QNetworkReply::setUrl(const QUrl &url)

Sets the URL being processed to be url. Normally, the URL matches that of the request that was posted, but for a variety of reasons it can be different (for example, a file path being made absolute or canonical).

See also url(), request(), and QNetworkRequest::url().

### `[protected, since 6.8]` void QNetworkReply::setWellKnownHeader(QHttpHeaders::WellKnownHeader name, QByteArrayView value)

Sets the header name to be of value value. If name was previously set, it is overridden.

This function was introduced in Qt 6.8.

### `[signal, since 6.3]` void QNetworkReply::socketStartedConnecting()

This signal is emitted 0 or more times, when the socket is connecting, before sending the request. Useful for custom progress or timeout handling.

This function was introduced in Qt 6.3.

See also metaDataChanged() and requestSent().

### `[signal]` void QNetworkReply::uploadProgress(qint64 bytesSent, qint64 bytesTotal)

This signal is emitted to indicate the progress of the upload part of this network request, if there's any. If there's no upload associated with this request, this signal will not be emitted.

The bytesSent parameter indicates the number of bytes uploaded, while bytesTotal indicates the total number of bytes to be uploaded. If the number of bytes to be uploaded could not be determined, bytesTotal will be -1.

The upload is finished when bytesSent is equal to bytesTotal. At that time, bytesTotal will not be -1.

See also downloadProgress().

### QUrl QNetworkReply::url() const

Returns the URL of the content downloaded or uploaded. Note that the URL may be different from that of the original request. If redirections were enabled in the request, then this function returns the current url that the network API is accessing, i.e the url of the resource the request got redirected to.

See also request(), setUrl(), QNetworkRequest::url(), and redirected().

### `[override virtual protected]` qint64 QNetworkReply::writeData(const char *data, qint64 len)

Reimplements: QIODevice::writeData(const char *data, qint64 maxSize).