QNetworkRequest Class

The QNetworkRequest class holds a request to be sent with QNetworkAccessManager. More...

Header: #include <QNetworkRequest>
qmake: QT += network
Since: Qt 4.4

This class was introduced in Qt 4.4.

Public Types

enum Attribute { HttpStatusCodeAttribute, HttpReasonPhraseAttribute, RedirectionTargetAttribute, ConnectionEncryptedAttribute, CacheLoadControlAttribute, …, UserMax }
enum CacheLoadControl { AlwaysNetwork, PreferNetwork, PreferCache, AlwaysCache }
enum KnownHeaders { ContentDispositionHeader, ContentTypeHeader, ContentLengthHeader, LocationHeader, LastModifiedHeader, …, ServerHeader }
enum LoadControl { Automatic, Manual }
enum Priority { HighPriority, NormalPriority, LowPriority }
enum RedirectPolicy { ManualRedirectPolicy, NoLessSafeRedirectPolicy, SameOriginRedirectPolicy, UserVerifiedRedirectPolicy }
enum TransferTimeoutConstant { DefaultTransferTimeoutConstant }

Detailed Description

QNetworkRequest is part of the Network Access API and is the class holding the information necessary to send a request over the network. It contains a URL and some ancillary information that can be used to modify the request.

See also QNetworkReply and QNetworkAccessManager.

Member Type Documentation

enum QNetworkRequest::Attribute

Attribute codes for the QNetworkRequest and QNetworkReply.

Attributes are extra meta-data that are used to control the behavior of the request and to pass further information from the reply back to the application. Attributes are also extensible, allowing custom implementations to pass custom values.

The following table explains what the default attribute codes are, the QVariant types associated, the default value if said attribute is missing and whether it's used in requests or replies.

ConstantValueDescription
QNetworkRequest::HttpStatusCodeAttribute0Replies only, type: QMetaType::Int (no default) Indicates the HTTP status code received from the HTTP server (like 200, 304, 404, 401, etc.). If the connection was not HTTP-based, this attribute will not be present.
QNetworkRequest::HttpReasonPhraseAttribute1Replies only, type: QMetaType::QByteArray (no default) Indicates the HTTP reason phrase as received from the HTTP server (like "Ok", "Found", "Not Found", "Access Denied", etc.) This is the human-readable representation of the status code (see above). If the connection was not HTTP-based, this attribute will not be present.
QNetworkRequest::RedirectionTargetAttribute2Replies only, type: QMetaType::QUrl (no default) If present, it indicates that the server is redirecting the request to a different URL. The Network Access API does not by default follow redirections: the application can determine if the requested redirection should be allowed, according to its security policies, or it can set QNetworkRequest::FollowRedirectsAttribute to true (in which case the redirection will be followed and this attribute will not be present in the reply). The returned URL might be relative. Use QUrl::resolved() to create an absolute URL out of it.
QNetworkRequest::ConnectionEncryptedAttribute3Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained through an encrypted (secure) connection.
QNetworkRequest::CacheLoadControlAttribute4Requests only, type: QMetaType::Int (default: QNetworkRequest::PreferNetwork) Controls how the cache should be accessed. The possible values are those of QNetworkRequest::CacheLoadControl. Note that the default QNetworkAccessManager implementation does not support caching. However, this attribute may be used by certain backends to modify their requests (for example, for caching proxies).
QNetworkRequest::CacheSaveControlAttribute5Requests only, type: QMetaType::Bool (default: true) Controls if the data obtained should be saved to cache for future uses. If the value is false, the data obtained will not be automatically cached. If true, data may be cached, provided it is cacheable (what is cacheable depends on the protocol being used).
QNetworkRequest::SourceIsFromCacheAttribute6Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained from cache or not.
QNetworkRequest::DoNotBufferUploadDataAttribute7Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to buffer the upload data, e.g. when doing a HTTP POST. When using this flag with sequential upload data, the ContentLengthHeader header must be set.
QNetworkRequest::HttpPipeliningAllowedAttribute8Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP pipelining with this request.
QNetworkRequest::HttpPipeliningWasUsedAttribute9Replies only, type: QMetaType::Bool Indicates whether the HTTP pipelining was used for receiving this reply.
QNetworkRequest::CustomVerbAttribute10Requests only, type: QMetaType::QByteArray Holds the value for the custom HTTP verb to send (destined for usage of other verbs than GET, POST, PUT and DELETE). This verb is set when calling QNetworkAccessManager::sendCustomRequest().
QNetworkRequest::CookieLoadControlAttribute11Requests only, type: QMetaType::Int (default: QNetworkRequest::Automatic) Indicates whether to send 'Cookie' headers in the request. This attribute is set to false by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.)
QNetworkRequest::CookieSaveControlAttribute13Requests only, type: QMetaType::Int (default: QNetworkRequest::Automatic) Indicates whether to save 'Cookie' headers received from the server in reply to the request. This attribute is set to false by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.)
QNetworkRequest::AuthenticationReuseAttribute12Requests only, type: QMetaType::Int (default: QNetworkRequest::Automatic) Indicates whether to use cached authorization credentials in the request, if available. If this is set to QNetworkRequest::Manual and the authentication mechanism is 'Basic' or 'Digest', Qt will not send an an 'Authorization' HTTP header with any cached credentials it may have for the request's URL. This attribute is set to QNetworkRequest::Manual by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.)
QNetworkRequest::BackgroundRequestAttribute17Type: QMetaType::Bool (default: false) Indicates that this is a background transfer, rather than a user initiated transfer. Depending on the platform, background transfers may be subject to different policies. The QNetworkSession ConnectInBackground property will be set according to this attribute.
QNetworkRequest::SpdyAllowedAttribute18Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use SPDY with this request. This applies only to SSL requests, and depends on the server supporting SPDY. Obsolete, use Http2 instead of Spdy.
QNetworkRequest::SpdyWasUsedAttribute19Replies only, type: QMetaType::Bool Indicates whether SPDY was used for receiving this reply. Obsolete, use Http2 instead of Spdy.
QNetworkRequest::Http2AllowedAttribute22Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP/2 with this request. This applies to SSL requests or 'cleartext' HTTP/2.
QNetworkRequest::Http2WasUsedAttribute23Replies only, type: QMetaType::Bool (default: false) Indicates whether HTTP/2 was used for receiving this reply. (This value was introduced in 5.9.)
QNetworkRequest::HTTP2AllowedAttributeHttp2AllowedAttributeObsolete alias for Http2AllowedAttribute.
QNetworkRequest::HTTP2WasUsedAttribute23Obsolete alias for Http2WasUsedAttribute.
QNetworkRequest::EmitAllUploadProgressSignalsAttributeBackgroundRequestAttribute + 3Requests only, type: QMetaType::Bool (default: false) Indicates whether all upload signals should be emitted. By default, the uploadProgress signal is emitted only in 100 millisecond intervals. (This value was introduced in 5.5.)
QNetworkRequest::FollowRedirectsAttribute21Requests only, type: QMetaType::Bool (default: false) Indicates whether the Network Access API should automatically follow a HTTP redirect response or not. Currently redirects that are insecure, that is redirecting from "https" to "http" protocol, are not allowed. (This value was introduced in 5.6.)
QNetworkRequest::OriginalContentLengthAttribute24Replies only, type QMetaType::Int Holds the original content-length attribute before being invalidated and removed from the header when the data is compressed and the request was marked to be decompressed automatically. (This value was introduced in 5.9.)
QNetworkRequest::RedirectPolicyAttribute25Requests only, type: QMetaType::Int, should be one of the QNetworkRequest::RedirectPolicy values (default: ManualRedirectPolicy). This attribute obsoletes FollowRedirectsAttribute. (This value was introduced in 5.9.)
QNetworkRequest::Http2DirectAttribute26Requests only, type: QMetaType::Bool (default: false) If set, this attribute will force QNetworkAccessManager to use HTTP/2 protocol without initial HTTP/2 protocol negotiation. Use of this attribute implies prior knowledge that a particular server supports HTTP/2. The attribute works with SSL or 'cleartext' HTTP/2. If a server turns out to not support HTTP/2, when HTTP/2 direct was specified, QNetworkAccessManager gives up, without attempting to fall back to HTTP/1.1. If both Http2AllowedAttribute and Http2DirectAttribute are set, Http2DirectAttribute takes priority. (This value was introduced in 5.11.)
QNetworkRequest::AutoDeleteReplyOnFinishAttribute28Requests only, type: QMetaType::Bool (default: false) If set, this attribute will make QNetworkAccessManager delete the QNetworkReply after having emitted "finished". (This value was introduced in 5.14.)
QNetworkRequest::User1000Special type. Additional information can be passed in QVariants with types ranging from User to UserMax. The default implementation of Network Access will ignore any request attributes in this range and it will not produce any attributes in this range in replies. The range is reserved for extensions of QNetworkAccessManager.
QNetworkRequest::UserMax32767Special type. See User.

This enum was introduced or modified in Qt 4.7.

enum QNetworkRequest::CacheLoadControl

Controls the caching mechanism of QNetworkAccessManager.

ConstantValueDescription
QNetworkRequest::AlwaysNetwork0always load from network and do not check if the cache has a valid entry (similar to the "Reload" feature in browsers); in addition, force intermediate caches to re-validate.
QNetworkRequest::PreferNetwork1default value; load from the network if the cached entry is older than the network entry. This will never return stale data from the cache, but revalidate resources that have become stale.
QNetworkRequest::PreferCache2load from cache if available, otherwise load from network. Note that this can return possibly stale (but not expired) items from cache.
QNetworkRequest::AlwaysCache3only load from cache, indicating error if the item was not cached (i.e., off-line mode)

enum QNetworkRequest::KnownHeaders

List of known header types that QNetworkRequest parses. Each known header is also represented in raw form with its full HTTP name.

ConstantValueDescription
QNetworkRequest::ContentDispositionHeader6Corresponds to the HTTP Content-Disposition header and contains a string containing the disposition type (for instance, attachment) and a parameter (for instance, filename).
QNetworkRequest::ContentTypeHeader0Corresponds to the HTTP Content-Type header and contains a string containing the media (MIME) type and any auxiliary data (for instance, charset).
QNetworkRequest::ContentLengthHeader1Corresponds to the HTTP Content-Length header and contains the length in bytes of the data transmitted.
QNetworkRequest::LocationHeader2Corresponds to the HTTP Location header and contains a URL representing the actual location of the data, including the destination URL in case of redirections.
QNetworkRequest::LastModifiedHeader3Corresponds to the HTTP Last-Modified header and contains a QDateTime representing the last modification date of the contents.
QNetworkRequest::IfModifiedSinceHeader9Corresponds to the HTTP If-Modified-Since header and contains a QDateTime. It is usually added to a QNetworkRequest. The server shall send a 304 (Not Modified) response if the resource has not changed since this time.
QNetworkRequest::ETagHeader10Corresponds to the HTTP ETag header and contains a QString representing the last modification state of the contents.
QNetworkRequest::IfMatchHeader11Corresponds to the HTTP If-Match header and contains a QStringList. It is usually added to a QNetworkRequest. The server shall send a 412 (Precondition Failed) response if the resource does not match.
QNetworkRequest::IfNoneMatchHeader12Corresponds to the HTTP If-None-Match header and contains a QStringList. It is usually added to a QNetworkRequest. The server shall send a 304 (Not Modified) response if the resource does match.
QNetworkRequest::CookieHeader4Corresponds to the HTTP Cookie header and contains a QList<QNetworkCookie> representing the cookies to be sent back to the server.
QNetworkRequest::SetCookieHeader5Corresponds to the HTTP Set-Cookie header and contains a QList<QNetworkCookie> representing the cookies sent by the server to be stored locally.
QNetworkRequest::UserAgentHeader7The User-Agent header sent by HTTP clients.
QNetworkRequest::ServerHeader8The Server header received by HTTP clients.

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

enum QNetworkRequest::LoadControl

Indicates if an aspect of the request's loading mechanism has been manually overridden, e.g. by Qt WebKit.

ConstantValueDescription
QNetworkRequest::Automatic0default value: indicates default behaviour.
QNetworkRequest::Manual1indicates behaviour has been manually overridden.

This enum was introduced or modified in Qt 4.7.

enum QNetworkRequest::Priority

This enum lists the possible network request priorities.

ConstantValueDescription
QNetworkRequest::HighPriority1High priority
QNetworkRequest::NormalPriority3Normal priority
QNetworkRequest::LowPriority5Low priority

This enum was introduced or modified in Qt 4.7.

enum QNetworkRequest::RedirectPolicy

Indicates whether the Network Access API should automatically follow a HTTP redirect response or not.

ConstantValueDescription
QNetworkRequest::ManualRedirectPolicy0Default value: not following any redirects.
QNetworkRequest::NoLessSafeRedirectPolicy1Only "http"->"http", "http" -> "https" or "https" -> "https" redirects are allowed. Equivalent to setting the old FollowRedirectsAttribute to true
QNetworkRequest::SameOriginRedirectPolicy2Require the same protocol, host and port. Note, http://example.com and http://example.com:80 will fail with this policy (implicit/explicit ports are considered to be a mismatch).
QNetworkRequest::UserVerifiedRedirectPolicy3Client decides whether to follow each redirect by handling the redirected() signal, emitting redirectAllowed() on the QNetworkReply object to allow the redirect or aborting/finishing it to reject the redirect. This can be used, for example, to ask the user whether to accept the redirect, or to decide based on some app-specific configuration.

Note: When Qt handles redirects it will, for legacy and compatibility reasons, issue the redirected request using GET when the server returns a 301 or 302 response, regardless of the original method used, unless it was HEAD.

This enum was introduced or modified in Qt 5.9.

enum QNetworkRequest::TransferTimeoutConstant

A constant that can be used for enabling transfer timeouts with a preset value.

ConstantValueDescription
QNetworkRequest::DefaultTransferTimeoutConstant30000The transfer timeout in milliseconds. Used if setTimeout() is called without an argument.

This enum was introduced or modified in Qt 5.15.