PySide6.QtNetwork.QHttpPart¶
- class QHttpPart¶
- The - QHttpPartclass holds a body part to be used inside a HTTP multipart MIME message.- Details- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - The - QHttpPartclass holds a body part to be used inside a HTTP multipart MIME message (which is represented by the- QHttpMultiPartclass). A- QHttpPartconsists 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 body - For setting headers, use - setHeader()and- setRawHeader(), which behave exactly like- setHeader()and- setRawHeader().- For reading small pieces of data, use - setBody(); for larger data blocks like e.g. images, use- setBodyDevice(). 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 via- post().- To construct a - QHttpPartwith a small body, consider the following snippet (this produces the data shown in the example above):- textPart = QHttpPart() 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 - QHttpPartreading from a device (e.g. a file), the following can be applied:- imagePart = QHttpPart() 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() file = QFile("image.jpg") file.open(QIODevice.OpenModeFlag.ReadOnly) imagePart.setBodyDevice(file) - Be aware that - QHttpPartdoes 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 for- QHttpMultiPart.- See also - Synopsis¶- Methods¶- def - __init__()
- def - __ne__()
- def - __eq__()
- def - setBody()
- def - setBodyDevice()
- def - setHeader()
- def - setRawHeader()
- def - swap()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - __init__()¶
 - Constructs an empty - QHttpPartobject.- __init__(other)
- Parameters:
- other – - QHttpPart
 
 - Creates a copy of - other.- Returns - trueif this object is not the same as- other.- See also - operator==()- Returns - trueif this object is the same as- other(i.e., if they have the same headers and body).- See also - operator!=()- setBody(body)¶
- Parameters:
- body – - QByteArray
 
 - Sets the body of this MIME part to - body. The body set with this method will be used unless the device is set via- setBodyDevice(). For a large amount of data (e.g. an image), use- setBodyDevice(), which will not copy the data internally.- See also - Sets the device to read the content from to - device. For large amounts of data this method should be preferred over- setBody(), because the content is not copied when using this method, but read directly from the device.- devicemust be open and readable.- QHttpPartdoes not take ownership of- device, i.e. the device must be closed and destroyed if necessary. if- deviceis sequential (e.g. sockets, but not files),- post()should be called after- devicehas emitted finished(). For unsetting the device and using data set via- setBody(), use “setBodyDevice(0)”.- setHeader(header, value)¶
- Parameters:
- header – - KnownHeaders
- value – object 
 
 
 - Sets the value of the known header - headerto be- value, overriding any previously set headers.- See also - setRawHeader(headerName, headerValue)¶
- Parameters:
- headerName – - QByteArray
- headerValue – - QByteArray
 
 
 - Sets the header - headerNameto be of value- headerValue. If- headerNamecorresponds to a known header (see- KnownHeaders), 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 - Swaps this HTTP part with - other. This operation is very fast and never fails.