QHttpPart¶
New in version 5.9.
Synopsis¶
Functions¶
def
__eq__
(other)def
__ne__
(other)def
setBody
(body)def
setBodyDevice
(device)def
setHeader
(header, value)def
setRawHeader
(headerName, headerValue)def
swap
(other)
Detailed Description¶
The
QHttpPart
class holds a body part to be used inside a HTTP multipart MIME message (which is represented by theQHttpMultiPart
class). AQHttpPart
consists of a header block and a data block, which are separated by each other by two consecutive new lines. An example for one part would be:Content-Type: text/plain Content-Disposition: form-data; name="text" here goes the bodyFor setting headers, use
setHeader()
andsetRawHeader()
, which behave exactly likesetHeader()
andsetRawHeader()
.For reading small pieces of data, use
setBody()
; for larger data blocks like e.g. images, usesetBodyDevice()
. The latter method saves memory by not copying the data internally, but reading directly from the device. This means that the device must be opened and readable at the moment when the multipart message containing the body part is sent on the network viapost()
.To construct a
QHttpPart
with a small body, consider the following snippet (this produces the data shown in the example above):QHttpPart textPart; textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\"")); textPart.setBody("here goes the body");To construct a
QHttpPart
reading from a device (e.g. a file), the following can be applied:QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg")); imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"")); imagePart.setRawHeader("Content-ID", "my@content.id"); // add any headers you like via setRawHeader() QFile *file = new QFile("image.jpg"); file->open(QIODevice::ReadOnly); imagePart.setBodyDevice(file);Be aware that
QHttpPart
does not take ownership of the device when set, so it is the developer’s responsibility to destroy it when it is not needed anymore. A good idea might be to set the multipart message as parent object for the device, as documented at the documentation forQHttpMultiPart
.See also
- class PySide2.QtNetwork.QHttpPart¶
PySide2.QtNetwork.QHttpPart(other)
- param other:
Constructs an empty
QHttpPart
object.Creates a copy of
other
.
- PySide2.QtNetwork.QHttpPart.__ne__(other)¶
- Parameters:
other –
PySide2.QtNetwork.QHttpPart
- Return type:
bool
Returns
true
if this object is not the same asother
.See also
operator==()
- PySide2.QtNetwork.QHttpPart.__eq__(other)¶
- Parameters:
other –
PySide2.QtNetwork.QHttpPart
- Return type:
bool
Returns
true
if this object is the same asother
(i.e., if they have the same headers and body).See also
operator!=()
- PySide2.QtNetwork.QHttpPart.setBody(body)¶
- Parameters:
body –
PySide2.QtCore.QByteArray
Sets the body of this MIME part to
body
. The body set with this method will be used unless the device is set viasetBodyDevice()
. For a large amount of data (e.g. an image), usesetBodyDevice()
, which will not copy the data internally.See also
- PySide2.QtNetwork.QHttpPart.setBodyDevice(device)¶
- Parameters:
device –
PySide2.QtCore.QIODevice
Sets the device to read the content from to
device
. For large amounts of data this method should be preferred oversetBody()
, because the content is not copied when using this method, but read directly from the device.device
must be open and readable.QHttpPart
does not take ownership ofdevice
, i.e. the device must be closed and destroyed if necessary. ifdevice
is sequential (e.g. sockets, but not files),post()
should be called afterdevice
has emitted finished(). For unsetting the device and using data set viasetBody()
, use “(0)”.
- PySide2.QtNetwork.QHttpPart.setHeader(header, value)¶
- Parameters:
header –
KnownHeaders
value – object
Sets the value of the known header
header
to bevalue
, overriding any previously set headers.See also
KnownHeaders
setRawHeader()
setHeader()
- PySide2.QtNetwork.QHttpPart.setRawHeader(headerName, headerValue)¶
- Parameters:
headerName –
PySide2.QtCore.QByteArray
headerValue –
PySide2.QtCore.QByteArray
Sets the header
headerName
to be of valueheaderValue
. IfheaderName
corresponds to a known header (seeKnownHeaders
), the raw format will be parsed and the corresponding “cooked” header will be set as well.Note
Setting the same header twice overrides the previous setting. To accomplish the behaviour of multiple HTTP headers of the same name, you should concatenate the two values, separating them with a comma (“,”) and set one single raw header.
See also
KnownHeaders
setHeader()
setRawHeader()
- PySide2.QtNetwork.QHttpPart.swap(other)¶
- Parameters:
other –
PySide2.QtNetwork.QHttpPart
Swaps this HTTP part with
other
. This function is very fast and never fails.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.