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
Status: Technical Preview

Public Types

HeaderList
enum class StatusCode { Continue, SwitchingProtocols, Processing, Ok, Created, …, NetworkConnectTimeoutError }

Public Functions

QHttpServerResponder(QHttpServerResponder &&other)
~QHttpServerResponder()
(since 6.5) void sendResponse(const QHttpServerResponse &response)
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)

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.

Member Type Documentation

[alias] QHttpServerResponder::HeaderList

Type alias for std::initializer_list<std::pair<QByteArray, QByteArray>>

enum class QHttpServerResponder::StatusCode

HTTP status codes

ConstantValue
QHttpServerResponder::StatusCode::Continue100
QHttpServerResponder::StatusCode::SwitchingProtocols101
QHttpServerResponder::StatusCode::Processing102
QHttpServerResponder::StatusCode::Ok200
QHttpServerResponder::StatusCode::Created201
QHttpServerResponder::StatusCode::Accepted202
QHttpServerResponder::StatusCode::NonAuthoritativeInformation203
QHttpServerResponder::StatusCode::NoContent204
QHttpServerResponder::StatusCode::ResetContent205
QHttpServerResponder::StatusCode::PartialContent206
QHttpServerResponder::StatusCode::MultiStatus207
QHttpServerResponder::StatusCode::AlreadyReported208
QHttpServerResponder::StatusCode::IMUsed226
QHttpServerResponder::StatusCode::MultipleChoices300
QHttpServerResponder::StatusCode::MovedPermanently301
QHttpServerResponder::StatusCode::Found302
QHttpServerResponder::StatusCode::SeeOther303
QHttpServerResponder::StatusCode::NotModified304
QHttpServerResponder::StatusCode::UseProxy305
QHttpServerResponder::StatusCode::TemporaryRedirect307
QHttpServerResponder::StatusCode::PermanentRedirect308
QHttpServerResponder::StatusCode::BadRequest400
QHttpServerResponder::StatusCode::Unauthorized401
QHttpServerResponder::StatusCode::PaymentRequired402
QHttpServerResponder::StatusCode::Forbidden403
QHttpServerResponder::StatusCode::NotFound404
QHttpServerResponder::StatusCode::MethodNotAllowed405
QHttpServerResponder::StatusCode::NotAcceptable406
QHttpServerResponder::StatusCode::ProxyAuthenticationRequired407
QHttpServerResponder::StatusCode::RequestTimeout408
QHttpServerResponder::StatusCode::Conflict409
QHttpServerResponder::StatusCode::Gone410
QHttpServerResponder::StatusCode::LengthRequired411
QHttpServerResponder::StatusCode::PreconditionFailed412
QHttpServerResponder::StatusCode::PayloadTooLarge413
QHttpServerResponder::StatusCode::UriTooLong414
QHttpServerResponder::StatusCode::UnsupportedMediaType415
QHttpServerResponder::StatusCode::RequestRangeNotSatisfiable416
QHttpServerResponder::StatusCode::ExpectationFailed417
QHttpServerResponder::StatusCode::ImATeapot418
QHttpServerResponder::StatusCode::MisdirectedRequest421
QHttpServerResponder::StatusCode::UnprocessableEntity422
QHttpServerResponder::StatusCode::Locked423
QHttpServerResponder::StatusCode::FailedDependency424
QHttpServerResponder::StatusCode::UpgradeRequired426
QHttpServerResponder::StatusCode::PreconditionRequired428
QHttpServerResponder::StatusCode::TooManyRequests429
QHttpServerResponder::StatusCode::RequestHeaderFieldsTooLarge431
QHttpServerResponder::StatusCode::UnavailableForLegalReasons451
QHttpServerResponder::StatusCode::InternalServerError500
QHttpServerResponder::StatusCode::NotImplemented501
QHttpServerResponder::StatusCode::BadGateway502
QHttpServerResponder::StatusCode::ServiceUnavailable503
QHttpServerResponder::StatusCode::GatewayTimeout504
QHttpServerResponder::StatusCode::HttpVersionNotSupported505
QHttpServerResponder::StatusCode::VariantAlsoNegotiates506
QHttpServerResponder::StatusCode::InsufficientStorage507
QHttpServerResponder::StatusCode::LoopDetected508
QHttpServerResponder::StatusCode::NotExtended510
QHttpServerResponder::StatusCode::NetworkAuthenticationRequired511
QHttpServerResponder::StatusCode::NetworkConnectTimeoutError599

Member Function Documentation

QHttpServerResponder::QHttpServerResponder(QHttpServerResponder &&other)

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

[noexcept] QHttpServerResponder::~QHttpServerResponder()

Destroys a QHttpServerResponder.

[since 6.5] void QHttpServerResponder::sendResponse(const QHttpServerResponse &response)

Sends a HTTP response to the client.

This function was introduced in Qt 6.5.

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)

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