CopperSpice API  1.9.1
QHttpPart Class Reference

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message. More...

Public Methods

 QHttpPart ()
 
 QHttpPart (const QHttpPart &other)
 
 ~QHttpPart ()
 
bool operator!= (const QHttpPart &other) const
 
QHttpPart & operator= (const QHttpPart &other)
 
QHttpPart & operator= (QHttpPart &&other)
 
bool operator== (const QHttpPart &other) const
 
void setBody (const QByteArray &body)
 
void setBodyDevice (QIODevice *device)
 
void setHeader (QNetworkRequest::KnownHeaders header, const QVariant &value)
 
void setRawHeader (const QByteArray &headerName, const QByteArray &headerValue)
 
void swap (QHttpPart &other)
 

Detailed Description

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message. The class consists of a header block and a data block, which are separated from each other by two consecutive new lines.

Content-Type: text/plain
Content-Disposition: form-data; name="text"
// body goes here

To set the headers use setHeader() and setRawHeader() which behave exactly like QNetworkRequest::setHeader() and QNetworkRequest::setRawHeader().

For small amounts of data use setBody(). For larger data blocks like 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 QNetworkAccessManager::post().

To construct a QHttpPart with a small body, consider the following code which 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("body goes here");

To construct a QHttpPart reading from a device like a file, the following can be used.

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);

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 for QHttpMultiPart.

See also
QHttpMultiPart, QNetworkAccessManager

Constructor & Destructor Documentation

QHttpPart::QHttpPart ( )

Constructs an empty QHttpPart object.

QHttpPart::QHttpPart ( const QHttpPart &  other)

Creates a copy of other.

QHttpPart::~QHttpPart ( )

Destroys this QHttpPart.

Method Documentation

bool QHttpPart::operator!= ( const QHttpPart &  other) const
inline

Returns true if this object is not the same as other.

See also
operator==()
QHttpPart & QHttpPart::operator= ( const QHttpPart &  other)

Copy assigns from other and returns a reference to this object.

QHttpPart & QHttpPart::operator= ( QHttpPart &&  other)
inline

Move assigns from other and returns a reference to this object.

bool QHttpPart::operator== ( const QHttpPart &  other) const

Returns true if this object is the same as other (i.e., if they have the same headers and body).

See also
operator!=()
void QHttpPart::setBody ( const QByteArray body)

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
setBodyDevice()
void QHttpPart::setBodyDevice ( QIODevice device)

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. device> must be open and readable. QHttpPart does not take ownership of device, i.e. the device must be closed and destroyed if necessary. if device is sequential (e.g. sockets, but not files).

The method QNetworkAccessManager::post() should be called after the device has emitted the finished() signal. For unsetting the device and using data set via setBody(), use "setBodyDevice(0)".

See also
setBody(), QNetworkAccessManager::post()
void QHttpPart::setHeader ( QNetworkRequest::KnownHeaders  header,
const QVariant value 
)

Sets the value of the known header to be value, overriding any previously set headers.

See also
QNetworkRequest::KnownHeaders, setRawHeader(), QNetworkRequest::setHeader()
void QHttpPart::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.

Setting the same header twice overrides the previous setting. To accomplish the behavior 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
QNetworkRequest::KnownHeaders, setHeader(), QNetworkRequest::setRawHeader()
void QHttpPart::swap ( QHttpPart &  other)
inline

Swaps other with this object. This operation is very fast and never fails.