Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QHttpPart Class Reference

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

#include <qhttpmultipart.h>

+ Collaboration diagram for QHttpPart:

Public Member Functions

 QHttpPart ()
 Constructs an empty QHttpPart object.
 
 QHttpPart (const QHttpPart &other)
 Creates a copy of other.
 
 ~QHttpPart ()
 Destroys this QHttpPart.
 
QHttpPartoperator= (QHttpPart &&other) noexcept
 
QHttpPartoperator= (const QHttpPart &other)
 Creates a copy of other.
 
void swap (QHttpPart &other) noexcept
 
bool 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).
 
bool operator!= (const QHttpPart &other) const
 Returns true if this object is not the same as other.
 
void setHeader (QNetworkRequest::KnownHeaders header, const QVariant &value)
 Sets the value of the known header header to be value, overriding any previously set headers.
 
void setRawHeader (const QByteArray &headerName, const QByteArray &headerValue)
 Sets the header headerName to be of value headerValue.
 
void setBody (const QByteArray &body)
 Sets the body of this MIME part to body.
 
void setBodyDevice (QIODevice *device)
 Sets the device to read the content from to device.
 

Friends

class QHttpMultiPartIODevice
 

Detailed Description

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

Since
4.8

\inmodule QtNetwork

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message (which is represented by the QHttpMultiPart class). A QHttpPart 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 body
QString text
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
Definition moc.h:24

For setting headers, use setHeader() and setRawHeader(), which behave exactly like QNetworkRequest::setHeader() and QNetworkRequest::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 QNetworkAccessManager::post().

To construct a QHttpPart with a small body, consider the following snippet (this produces the data shown in the example above):

textPart.setBody("here goes the body");
The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message.
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
Sets the value of the known header header to be value, overriding any previously set headers.
void setBody(const QByteArray &body)
Sets the body of this MIME part to body.
\inmodule QtCore
Definition qvariant.h:64

To construct a QHttpPart reading from a device (e.g. a file), the following can be applied:

imagePart.setRawHeader("Content-ID", "my@content.id"); // add any headers you like via setRawHeader()
QFile *file = new QFile("image.jpg");
\inmodule QtCore
Definition qfile.h:93
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:881
void setBodyDevice(QIODevice *device)
Sets the device to read the content from to device.
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
Sets the header headerName to be of value headerValue.
QFile file
[0]

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

See also
QHttpMultiPart, QNetworkAccessManager

Definition at line 23 of file qhttpmultipart.h.

Constructor & Destructor Documentation

◆ QHttpPart() [1/2]

QHttpPart::QHttpPart ( )

Constructs an empty QHttpPart object.

Definition at line 62 of file qhttpmultipart.cpp.

◆ QHttpPart() [2/2]

QHttpPart::QHttpPart ( const QHttpPart other)

Creates a copy of other.

Definition at line 69 of file qhttpmultipart.cpp.

◆ ~QHttpPart()

QHttpPart::~QHttpPart ( )

Destroys this QHttpPart.

Definition at line 76 of file qhttpmultipart.cpp.

Member Function Documentation

◆ operator!=()

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

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

See also
operator==()

Definition at line 35 of file qhttpmultipart.h.

References operator==(), and other().

+ Here is the call graph for this function:

◆ operator=() [1/2]

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

Creates a copy of other.

Definition at line 84 of file qhttpmultipart.cpp.

References other().

+ Here is the call graph for this function:

◆ operator=() [2/2]

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

Definition at line 29 of file qhttpmultipart.h.

References other(), and swap().

+ Here is the call graph for this function:

◆ operator==()

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!=()

Definition at line 104 of file qhttpmultipart.cpp.

References other().

+ Here is the call graph for this function:

◆ setBody()

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

Definition at line 154 of file qhttpmultipart.cpp.

References QHttpPartPrivate::setBody().

+ Here is the call graph for this function:

◆ 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), QNetworkAccessManager::post() should be called after device has emitted finished(). For unsetting the device and using data set via setBody(), use "setBodyDevice(0)".

See also
setBody(), QNetworkAccessManager::post()

Definition at line 174 of file qhttpmultipart.cpp.

References device, and QHttpPartPrivate::setBodyDevice().

+ Here is the call graph for this function:

◆ setHeader()

void QHttpPart::setHeader ( QNetworkRequest::KnownHeaders  header,
const QVariant value 
)

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

See also
QNetworkRequest::KnownHeaders, setRawHeader(), QNetworkRequest::setHeader()

Definition at line 123 of file qhttpmultipart.cpp.

References header(), and QNetworkHeadersPrivate::setCookedHeader().

+ Here is the call graph for this function:

◆ setRawHeader()

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.

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
QNetworkRequest::KnownHeaders, setHeader(), QNetworkRequest::setRawHeader()

Definition at line 141 of file qhttpmultipart.cpp.

References headerName(), headerValue(), and QNetworkHeadersPrivate::setRawHeader().

+ Here is the call graph for this function:

◆ swap()

void QHttpPart::swap ( QHttpPart other)
inlinenoexcept
Since
5.0

Swaps this HTTP part with other. This function is very fast and never fails.

Definition at line 32 of file qhttpmultipart.h.

References d, and other().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QHttpMultiPartIODevice

friend class QHttpMultiPartIODevice
friend

Definition at line 47 of file qhttpmultipart.h.


The documentation for this class was generated from the following files: