QHttpServerResponder Class

API for sending replies from an HTTP server. More...

Header: #include <QHttpServerResponder>
CMake: find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
qmake: QT += httpserver
Since: Qt 6.4

Public Functions

QHttpServerResponder(QHttpServerResponder &&other)
~QHttpServerResponder()
QTcpSocket *socket() const
void write(QIODevice *data, QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)
void write(QIODevice *data, const QByteArray &mimeType, QHttpServerResponder::StatusCode status = StatusCode::Ok)
void write(const QJsonDocument &document, QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)
void write(const QJsonDocument &document, QHttpServerResponder::StatusCode status = StatusCode::Ok)
void write(const QByteArray &data, QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)
void write(const QByteArray &data, const QByteArray &mimeType, QHttpServerResponder::StatusCode status = StatusCode::Ok)
void write(QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)
void write(QHttpServerResponder::StatusCode status = StatusCode::Ok)
void writeBody(const char *body, qint64 size)
void writeBody(const char *body)
void writeBody(const QByteArray &body)
void writeHeader(const QByteArray &header, const QByteArray &value)
void writeHeaders(QHttpServerResponder::HeaderList headers)
void writeStatusLine(QHttpServerResponder::StatusCode status = StatusCode::Ok, const QPair<quint8, quint8> &version = qMakePair(1u, 1u))

Detailed Description

Provides functions for writing back to an HTTP client with overloads for serializing JSON objects. It also has support for writing HTTP headers and status code. This class can be constructed by calling the protected static function makeResponder() in the QAbstractHttpServer class.

Member Function Documentation

QHttpServerResponder::QHttpServerResponder(QHttpServerResponder &&other)

Move-constructs a QHttpServerResponder instance, making it point at the same object that other was pointing to.

QHttpServerResponder::~QHttpServerResponder()

Destroys a QHttpServerResponder.

QTcpSocket *QHttpServerResponder::socket() const

Returns the socket used.

void QHttpServerResponder::write(QIODevice *data, QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status and HTTP headers headers. The I/O device data provides the body of the response. If data is sequential, the body of the message is sent in chunks: otherwise, the function assumes all the content is available and sends it all at once but the read is done in chunks.

Note: This function takes the ownership of data.

void QHttpServerResponder::write(QIODevice *data, const QByteArray &mimeType, QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status and a MIME type mimeType. The I/O device data provides the body of the response. If data is sequential, the body of the message is sent in chunks: otherwise, the function assumes all the content is available and sends it all at once but the read is done in chunks.

Note: This function takes the ownership of data.

void QHttpServerResponder::write(const QJsonDocument &document, QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status, JSON document document and HTTP headers headers.

Note: This function sets HTTP Content-Type header as "application/json".

void QHttpServerResponder::write(const QJsonDocument &document, QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status, and JSON document document.

Note: This function sets HTTP Content-Type header as "application/json".

void QHttpServerResponder::write(const QByteArray &data, QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status, HTTP Headers headers and a body data.

Note: This function sets HTTP Content-Length header.

void QHttpServerResponder::write(const QByteArray &data, const QByteArray &mimeType, QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status, a MIME type mimeType and a body data.

void QHttpServerResponder::write(QHttpServerResponder::HeaderList headers, QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status and HTTP Headers headers.

void QHttpServerResponder::write(QHttpServerResponder::StatusCode status = StatusCode::Ok)

Answers a request with an HTTP status code status.

Note: This function sets HTTP Content-Type header as "application/x-empty".

void QHttpServerResponder::writeBody(const char *body, qint64 size)

This function writes HTTP body body with size size.

void QHttpServerResponder::writeBody(const char *body)

This function writes HTTP body body.

void QHttpServerResponder::writeBody(const QByteArray &body)

This function writes HTTP body body.

void QHttpServerResponder::writeHeader(const QByteArray &header, const QByteArray &value)

This function writes an HTTP header header with value.

void QHttpServerResponder::writeHeaders(QHttpServerResponder::HeaderList headers)

This function writes HTTP headers headers.

void QHttpServerResponder::writeStatusLine(QHttpServerResponder::StatusCode status = StatusCode::Ok, const QPair<quint8, quint8> &version = qMakePair(1u, 1u))

This function writes HTTP status line with an HTTP status code status and an HTTP version version.

© 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.