![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager. More...
#include <qnetworkrequest.h>
Public Member Functions | |
QNetworkRequest () | |
Constructs a QNetworkRequest object with no URL to be requested. | |
QNetworkRequest (const QUrl &url) | |
Constructs a QNetworkRequest object with url as the URL to be requested. | |
QNetworkRequest (const QNetworkRequest &other) | |
Creates a copy of other. | |
~QNetworkRequest () | |
Disposes of the QNetworkRequest object. | |
QNetworkRequest & | operator= (QNetworkRequest &&other) noexcept |
QNetworkRequest & | operator= (const QNetworkRequest &other) |
Creates a copy of other. | |
void | swap (QNetworkRequest &other) noexcept |
bool | operator== (const QNetworkRequest &other) const |
Returns true if this object is the same as other (i.e., if they have the same URL, same headers and same meta-data settings). | |
bool | operator!= (const QNetworkRequest &other) const |
Returns false if this object is not the same as other. | |
QUrl | url () const |
Returns the URL this network request is referring to. | |
void | setUrl (const QUrl &url) |
Sets the URL this network request is referring to be url. | |
QHttpHeaders | headers () const |
void | setHeaders (const QHttpHeaders &newHeaders) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
void | setHeaders (QHttpHeaders &&newHeaders) |
QVariant | header (KnownHeaders header) const |
Returns the value of the known network header header if it is present in this request. | |
void | setHeader (KnownHeaders header, const QVariant &value) |
Sets the value of the known header header to be value, overriding any previously set headers. | |
bool | hasRawHeader (QAnyStringView headerName) const |
Returns true if the raw header headerName is present in this network request. | |
QList< QByteArray > | rawHeaderList () const |
Returns a list of all raw headers that are set in this network request. | |
QByteArray | rawHeader (QAnyStringView headerName) const |
Returns the raw form of header headerName. | |
void | setRawHeader (const QByteArray &headerName, const QByteArray &value) |
Sets the header headerName to be of value headerValue. | |
QVariant | attribute (Attribute code, const QVariant &defaultValue=QVariant()) const |
Returns the attribute associated with the code code. | |
void | setAttribute (Attribute code, const QVariant &value) |
Sets the attribute associated with code code to be value value. | |
QSslConfiguration | sslConfiguration () const |
Returns this network request's SSL configuration. | |
void | setSslConfiguration (const QSslConfiguration &configuration) |
Sets this network request's SSL configuration to be config. | |
void | setOriginatingObject (QObject *object) |
QObject * | originatingObject () const |
Priority | priority () const |
void | setPriority (Priority priority) |
int | maximumRedirectsAllowed () const |
void | setMaximumRedirectsAllowed (int maximumRedirectsAllowed) |
QString | peerVerifyName () const |
void | setPeerVerifyName (const QString &peerName) |
Static Public Attributes | |
static constexpr auto | DefaultTransferTimeout |
Friends | |
class | QNetworkRequestPrivate |
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
\inmodule QtNetwork
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.
Definition at line 25 of file qnetworkrequest.h.
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.
\value HttpStatusCodeAttribute Replies 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.
\value HttpReasonPhraseAttribute Replies 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. {Note:} The reason phrase is not used when using HTTP/2.
\value RedirectionTargetAttribute Replies 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 follow redirections by default, unless QNetworkRequest::ManualRedirectPolicy is used. Additionally, if QNetworkRequest::UserVerifiedRedirectPolicy is used, then this attribute will be set if the redirect was not followed. The returned URL might be relative. Use QUrl::resolved() to create an absolute URL out of it.
\value ConnectionEncryptedAttribute Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained through an encrypted (secure) connection.
\value CacheLoadControlAttribute Requests 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).
\value CacheSaveControlAttribute Requests 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).
\value SourceIsFromCacheAttribute Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained from cache or not.
\value DoNotBufferUploadDataAttribute Requests 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.
\value HttpPipeliningAllowedAttribute Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP pipelining with this request.
\value HttpPipeliningWasUsedAttribute Replies only, type: QMetaType::Bool Indicates whether the HTTP pipelining was used for receiving this reply.
\value CustomVerbAttribute Requests 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().
\value CookieLoadControlAttribute Requests 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 \l{http://www.w3.org/TR/XMLHttpRequest2/#credentials-flag}{here} for more information. (This value was introduced in 4.7.)
\value CookieSaveControlAttribute Requests 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 \l{http://www.w3.org/TR/XMLHttpRequest2/#credentials-flag} {here} for more information. (This value was introduced in 4.7.)
\value AuthenticationReuseAttribute Requests 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 '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 \l{http://www.w3.org/TR/XMLHttpRequest2/#credentials-flag} {here} for more information. (This value was introduced in 4.7.)
\omitvalue MaximumDownloadBufferSizeAttribute
\omitvalue DownloadBufferAttribute
\omitvalue SynchronousRequestAttribute
\value BackgroundRequestAttribute Type: 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.
\value Http2AllowedAttribute Requests only, type: QMetaType::Bool (default: true) Indicates whether the QNetworkAccessManager code is allowed to use HTTP/2 with this request. This applies to SSL requests or 'cleartext' HTTP/2 if Http2CleartextAllowedAttribute is set.
\value Http2WasUsedAttribute Replies only, type: QMetaType::Bool (default: false) Indicates whether HTTP/2 was used for receiving this reply. (This value was introduced in 5.9.)
\value EmitAllUploadProgressSignalsAttribute Requests 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.)
\value OriginalContentLengthAttribute Replies 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.)
\value RedirectPolicyAttribute Requests only, type: QMetaType::Int, should be one of the QNetworkRequest::RedirectPolicy values (default: NoLessSafeRedirectPolicy). (This value was introduced in 5.9.)
\value Http2DirectAttribute Requests 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 with 'cleartext' HTTP/2 if Http2CleartextAllowedAttribute is set. 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.)
\omitvalue ResourceTypeAttribute
\value AutoDeleteReplyOnFinishAttribute Requests 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.)
\value ConnectionCacheExpiryTimeoutSecondsAttribute Requests only, type: QMetaType::Int To set when the TCP connections to a server (HTTP1 and HTTP2) should be closed after the last pending request had been processed. (This value was introduced in 6.3.)
\value Http2CleartextAllowedAttribute Requests only, type: QMetaType::Bool (default: false) If set, this attribute will tell QNetworkAccessManager to attempt an upgrade to HTTP/2 over cleartext (also known as h2c). Until Qt 7 the default value for this attribute can be overridden to true by setting the QT_NETWORK_H2C_ALLOWED environment variable. This attribute is ignored if the Http2AllowedAttribute is not set. (This value was introduced in 6.3.)
\value UseCredentialsAttribute Requests only, type: QMetaType::Bool (default: false) Indicates if the underlying XMLHttpRequest cross-site Access-Control requests should be made using credentials. Has no effect on same-origin requests. This only affects the WebAssembly platform. (This value was introduced in 6.5.)
\value FullLocalServerNameAttribute Requests only, type: QMetaType::String Holds the full local server name to be used for the underlying QLocalSocket. This attribute is used by the QNetworkAccessManager to connect to a specific local server, when QLocalSocket's behavior for a simple name isn't enough. The URL in the QNetworkRequest must still use unix+http: or local+http: scheme. And the hostname in the URL will be used for the Host header in the HTTP request. (This value was introduced in 6.8.)
\value User Special 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.
\value UserMax Special type. See User.
Definition at line 47 of file qnetworkrequest.h.
Controls the caching mechanism of QNetworkAccessManager.
\value AlwaysNetwork always 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.
\value PreferNetwork default 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.
\value PreferCache load from cache if available, otherwise load from network. Note that this can return possibly stale (but not expired) items from cache.
\value AlwaysCache only load from cache, indicating error if the item was not cached (i.e., off-line mode)
Enumerator | |
---|---|
AlwaysNetwork | |
PreferNetwork | |
PreferCache | |
AlwaysCache |
Definition at line 82 of file qnetworkrequest.h.
List of known header types that QNetworkRequest parses.
Each known header is also represented in raw form with its full HTTP name.
\value ContentDispositionHeader Corresponds to the HTTP Content-Disposition header and contains a string containing the disposition type (for instance, attachment) and a parameter (for instance, filename).
\value ContentTypeHeader Corresponds to the HTTP Content-Type header and contains a string containing the media (MIME) type and any auxiliary data (for instance, charset).
\value ContentLengthHeader Corresponds to the HTTP Content-Length header and contains the length in bytes of the data transmitted.
\value LocationHeader Corresponds to the HTTP Location header and contains a URL representing the actual location of the data, including the destination URL in case of redirections.
\value LastModifiedHeader Corresponds to the HTTP Last-Modified header and contains a QDateTime representing the last modification date of the contents.
\value IfModifiedSinceHeader Corresponds 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.
\value ETagHeader Corresponds to the HTTP ETag header and contains a QString representing the last modification state of the contents.
\value IfMatchHeader Corresponds 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.
\value IfNoneMatchHeader Corresponds 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.
\value CookieHeader Corresponds to the HTTP Cookie header and contains a QList<QNetworkCookie> representing the cookies to be sent back to the server.
\value SetCookieHeader Corresponds to the HTTP Set-Cookie header and contains a QList<QNetworkCookie> representing the cookies sent by the server to be stored locally.
\value UserAgentHeader The User-Agent header sent by HTTP clients.
\value ServerHeader The Server header received by HTTP clients.
\omitvalue NumKnownHeaders
Definition at line 29 of file qnetworkrequest.h.
Indicates if an aspect of the request's loading mechanism has been manually overridden, e.g. by Qt WebKit.
\value Automatic default value: indicates default behaviour.
\value Manual indicates behaviour has been manually overridden.
Enumerator | |
---|---|
Automatic | |
Manual |
Definition at line 88 of file qnetworkrequest.h.
This enum lists the possible network request priorities.
\value HighPriority High priority \value NormalPriority Normal priority \value LowPriority Low priority
Enumerator | |
---|---|
HighPriority | |
NormalPriority | |
LowPriority |
Definition at line 93 of file qnetworkrequest.h.
Indicates whether the Network Access API should automatically follow a HTTP redirect response or not.
\value ManualRedirectPolicy Not following any redirects.
\value NoLessSafeRedirectPolicy Default value: Only "http"->"http", "http" -> "https" or "https" -> "https" redirects are allowed.
\value SameOriginRedirectPolicy Require 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).
\value UserVerifiedRedirectPolicy Client 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.
Enumerator | |
---|---|
ManualRedirectPolicy | |
NoLessSafeRedirectPolicy | |
SameOriginRedirectPolicy | |
UserVerifiedRedirectPolicy |
Definition at line 99 of file qnetworkrequest.h.
A constant that can be used for enabling transfer timeouts with a preset value.
\value DefaultTransferTimeoutConstant The transfer timeout in milliseconds. Used if setTimeout() is called without an argument.
Enumerator | |
---|---|
DefaultTransferTimeoutConstant |
Definition at line 106 of file qnetworkrequest.h.
QNetworkRequest::QNetworkRequest | ( | ) |
Constructs a QNetworkRequest object with no URL to be requested.
Use setUrl() to set one.
Definition at line 519 of file qnetworkrequest.cpp.
|
explicit |
Constructs a QNetworkRequest object with url as the URL to be requested.
Definition at line 540 of file qnetworkrequest.cpp.
QNetworkRequest::QNetworkRequest | ( | const QNetworkRequest & | other | ) |
Creates a copy of other.
Definition at line 549 of file qnetworkrequest.cpp.
QNetworkRequest::~QNetworkRequest | ( | ) |
Disposes of the QNetworkRequest object.
Definition at line 557 of file qnetworkrequest.cpp.
QVariant QNetworkRequest::attribute | ( | Attribute | code, |
const QVariant & | defaultValue = QVariant() ) const |
Returns the attribute associated with the code code.
If the attribute has not been set, it returns defaultValue.
Definition at line 753 of file qnetworkrequest.cpp.
bool QNetworkRequest::hasRawHeader | ( | QAnyStringView | headerName | ) | const |
Returns true
if the raw header headerName is present in this network request.
Definition at line 685 of file qnetworkrequest.cpp.
QVariant QNetworkRequest::header | ( | KnownHeaders | header | ) | const |
Returns the value of the known network header header if it is present in this request.
If it is not present, returns QVariant() (i.e., an invalid variant).
Definition at line 661 of file qnetworkrequest.cpp.
QHttpHeaders QNetworkRequest::headers | ( | ) | const |
Returns headers that are set in this network request.
Definition at line 624 of file qnetworkrequest.cpp.
int QNetworkRequest::maximumRedirectsAllowed | ( | ) | const |
Returns the maximum number of redirects allowed to be followed for this request.
Definition at line 882 of file qnetworkrequest.cpp.
|
inline |
Returns false
if this object is not the same as other.
Definition at line 123 of file qnetworkrequest.h.
QNetworkRequest & QNetworkRequest::operator= | ( | const QNetworkRequest & | other | ) |
Creates a copy of other.
Definition at line 585 of file qnetworkrequest.cpp.
|
inlinenoexcept |
Definition at line 117 of file qnetworkrequest.h.
bool QNetworkRequest::operator== | ( | const QNetworkRequest & | other | ) | const |
Returns true
if this object is the same as other (i.e., if they have the same URL, same headers and same meta-data settings).
Definition at line 569 of file qnetworkrequest.cpp.
QObject * QNetworkRequest::originatingObject | ( | ) | const |
Returns a reference to the object that initiated this network request; returns \nullptr if not set or the object has been destroyed.
Definition at line 830 of file qnetworkrequest.cpp.
QString QNetworkRequest::peerVerifyName | ( | ) | const |
Returns the host name set for the certificate validation, as set by setPeerVerifyName. By default this returns a null string.
Definition at line 908 of file qnetworkrequest.cpp.
QNetworkRequest::Priority QNetworkRequest::priority | ( | ) | const |
Return the priority of this request.
Definition at line 842 of file qnetworkrequest.cpp.
QByteArray QNetworkRequest::rawHeader | ( | QAnyStringView | headerName | ) | const |
Returns the raw form of header headerName.
If no such header is present, an empty QByteArray is returned, which may be indistinguishable from a header that is present but has no content (use hasRawHeader() to find out if the header exists or not).
Raw headers can be set with setRawHeader() or with setHeader().
Definition at line 701 of file qnetworkrequest.cpp.
QList< QByteArray > QNetworkRequest::rawHeaderList | ( | ) | const |
Returns a list of all raw headers that are set in this network request.
The list is in the order that the headers were set.
Definition at line 712 of file qnetworkrequest.cpp.
Sets the attribute associated with code code to be value value.
If the attribute is already set, the previous value is discarded. In special, if value is an invalid QVariant, the attribute is unset.
Definition at line 766 of file qnetworkrequest.cpp.
void QNetworkRequest::setHeader | ( | KnownHeaders | header, |
const QVariant & | value ) |
Sets the value of the known header header to be value, overriding any previously set headers.
This operation also sets the equivalent raw HTTP header.
Definition at line 673 of file qnetworkrequest.cpp.
void QNetworkRequest::setHeaders | ( | const QHttpHeaders & | newHeaders | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 649 of file qnetworkrequest.cpp.
void QNetworkRequest::setHeaders | ( | QHttpHeaders && | newHeaders | ) |
Sets newHeaders as headers in this network request, overriding any previously set headers.
If some headers correspond to the known headers, the values will be parsed and the corresponding parsed form will also be set.
Definition at line 640 of file qnetworkrequest.cpp.
void QNetworkRequest::setMaximumRedirectsAllowed | ( | int | maxRedirectsAllowed | ) |
Sets the maximum number of redirects allowed to be followed for this request to maxRedirectsAllowed.
Definition at line 895 of file qnetworkrequest.cpp.
Allows setting a reference to the object initiating the request.
For example Qt WebKit sets the originating object to the QWebFrame that initiated the request.
Definition at line 816 of file qnetworkrequest.cpp.
Sets peerName as host name for the certificate validation, instead of the one used for the TCP connection.
Definition at line 921 of file qnetworkrequest.cpp.
Set the priority of this request to priority.
Definition at line 869 of file qnetworkrequest.cpp.
void QNetworkRequest::setRawHeader | ( | const QByteArray & | headerName, |
const QByteArray & | headerValue ) |
Sets the header headerName to be of value headerValue.
If headerName corresponds to a known header (see QNetworkRequest::KnownHeaders), the raw format will be parsed and the corresponding "cooked" header will be set as well.
For example:
will also set the known header LastModifiedHeader to be the QDateTime object of the parsed date.
Definition at line 739 of file qnetworkrequest.cpp.
void QNetworkRequest::setSslConfiguration | ( | const QSslConfiguration & | config | ) |
Sets this network request's SSL configuration to be config.
The settings that apply are the private key, the local certificate, the TLS protocol (e.g. TLS 1.3), the CA certificates and the ciphers that the SSL backend is allowed to use.
Definition at line 796 of file qnetworkrequest.cpp.
Sets the URL this network request is referring to be url.
Definition at line 612 of file qnetworkrequest.cpp.
QSslConfiguration QNetworkRequest::sslConfiguration | ( | ) | const |
Returns this network request's SSL configuration.
By default this is the same as QSslConfiguration::defaultConfiguration().
Definition at line 781 of file qnetworkrequest.cpp.
|
inlinenoexcept |
Definition at line 120 of file qnetworkrequest.h.
QUrl QNetworkRequest::url | ( | ) | const |
Returns the URL this network request is referring to.
Definition at line 602 of file qnetworkrequest.cpp.
|
friend |
Definition at line 192 of file qnetworkrequest.h.
|
staticconstexpr |
Definition at line 110 of file qnetworkrequest.h.