QHttpServerResponse Class
The QHttpServerResponse class holds a complete response: a status code, header fields and a body. More…
| Header | #include <QHttpServerResponse> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::HttpServer) |
| Module | Nt HTTP Server C++ Classes |
| Since | 6.4 |
List of all members, including inherited members
Public Types
| typedef | StatusCode |
Public Functions
| QHttpServerResponse(StatusCode statusCode) | |
| QHttpServerResponse(const char *data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(const QString &data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(const QByteArray &data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(QByteArray &&data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(const QJsonObject &data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(const QJsonArray &data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(const QByteArray &mimeType, const QByteArray &data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(const QByteArray &mimeType, QByteArray &&data, StatusCode status = StatusCode::Ok) | |
| QHttpServerResponse(QHttpServerResponse &&other) | |
| QHttpServerResponse & | operator=(QHttpServerResponse &&other) |
| ~QHttpServerResponse() | |
| QByteArray | data() const |
| QByteArray | mimeType() const |
| StatusCode | statusCode() const |
| QHttpHeaders | headers() const |
| void | setHeaders(const QHttpHeaders &newHeaders) |
| void | setHeaders(QHttpHeaders &&newHeaders) |
| void | swap(QHttpServerResponse &other) |
Static Public Members
Detailed Description
A route handler of QHttpServer usually returns a QHttpServerResponse, or a value that converts to one: a string, a JSON object or array, or a status code. The server adds the Content-Length header and the framing when it sends the response.
server.route("/teapot", [] { return QHttpServerResponse::StatusCode::ImATeapot; });
server.route("/version", [] { return QJsonObject{{"version", QT_VERSION_STR}}; });
server.route("/robots.txt", [] { return QHttpServerResponse("text/plain", "User-agent: *\nDisallow: /\n"); });
server.route("/source", [] { return QHttpServerResponse::fromFile(QStringLiteral(__FILE__)); });
Each constructor sets the Content-Type header from the kind of data:
Data | Content type |
|---|---|
QString, |
|
| |
guessed from the first bytes (see below) | |
StatusCode only |
|
For a QByteArray, the type is text/html if the data starts with an HTML document type or tag, image/png, image/jpeg, image/gif or application/pdf for those signatures, text/plain if it has no control characters other than white space, and application/octet-stream otherwise. Pass the type explicitly to avoid the guess.
See also QHttpServerResponder::sendResponse() and QHttpServer::addAfterRequestHandler().
Differences from Qt
The content type of a QByteArray and of fromFile() is guessed from a small built-in table, not from a MIME database. There is no QHttpServerResponse(QHttpServerResponse::StatusCode) for a QFuture, since Nt has no QFuture.
Member Type Documentation
typedef QHttpServerResponse::StatusCode
The same enum as QHttpServerResponder::StatusCode.
Member Function Documentation
QHttpServerResponse::QHttpServerResponse(StatusCode statusCode)
Constructs a response with the status statusCode and no body.
QHttpServerResponse::QHttpServerResponse(const char *data, StatusCode status = StatusCode::Ok)
Constructs a text/plain response with the status status whose body is the null-terminated string data.
QHttpServerResponse::QHttpServerResponse(const QString &data, StatusCode status = StatusCode::Ok)
Constructs a text/plain response with the status status whose body is data encoded in UTF-8.
QHttpServerResponse::QHttpServerResponse(const QByteArray &data, StatusCode status = StatusCode::Ok)
Constructs a response with the status status whose body is data. The content type is guessed from the data.
QHttpServerResponse::QHttpServerResponse(QByteArray &&data, StatusCode status = StatusCode::Ok)
This is an overloaded function.
QHttpServerResponse::QHttpServerResponse(const QJsonObject &data, StatusCode status = StatusCode::Ok)
Constructs an application/json response with the status status whose body is data.
QHttpServerResponse::QHttpServerResponse(const QJsonArray &data, StatusCode status = StatusCode::Ok)
This is an overloaded function.
QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, const QByteArray &data, StatusCode status = StatusCode::Ok)
Constructs a response with the status status whose body is data and whose content type is mimeType.
QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, QByteArray &&data, StatusCode status = StatusCode::Ok)
This is an overloaded function.
QHttpServerResponse::QHttpServerResponse(QHttpServerResponse &&other)
Move-constructs a response from other.
QHttpServerResponse::~QHttpServerResponse()
Destroys the response.
QByteArray QHttpServerResponse::data() const
Returns the body of the response.
QHttpServerResponse QHttpServerResponse::fromFile(const QString &fileName)
Returns a response whose body is the contents of the file fileName, or a 404 Not Found response if the file cannot be read. The content type is chosen from the file name's suffix, or guessed from the data.
The whole file is read into memory. To send a large file, pass a QFile to QHttpServerResponder::write() instead, which sends it in pieces.
QHttpHeaders QHttpServerResponse::headers() const
Returns the header fields of the response, including Content-Type.
See also setHeaders().
QByteArray QHttpServerResponse::mimeType() const
Returns the value of the Content-Type header, or an empty QByteArray if there is none.
See also headers().
QHttpServerResponse & QHttpServerResponse::operator=(QHttpServerResponse &&other)
Move-assigns other to this response and returns a reference to it.
void QHttpServerResponse::setHeaders(const QHttpHeaders &newHeaders)
Replaces the header fields of the response with newHeaders. Include Content-Type in them to keep a content type.
Content-Length, Transfer-Encoding and Connection are set by the server when it sends the response; those in newHeaders are ignored.
See also headers().
void QHttpServerResponse::setHeaders(QHttpHeaders &&newHeaders)
This is an overloaded function.
StatusCode QHttpServerResponse::statusCode() const
Returns the status code of the response.
void QHttpServerResponse::swap(QHttpServerResponse &other)
Swaps this response with other.