QHttpServerResponse Class | Qt HTTP Server 6.8.0
Description: Encapsulates an HTTP response.

- Qt 6.8
- Qt HTTP Server
- C++ Classes
- QtHttpServer (Technical Preview)
- QHttpServerResponse
- Qt 6.8.0 Reference Documentation

# QHttpServerResponse Class

Encapsulates an HTTP response. More...

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

- List of all members, including inherited members

## Public Types

| StatusCode

## Public Functions

| QHttpServerResponse(const QHttpServerResponse::StatusCode statusCode)
| QHttpServerResponse(QByteArray &&data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(const QByteArray &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(const QJsonArray &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(const QJsonObject &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(const QString &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(const char *data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(const QByteArray &mimeType, QByteArray &&data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(const QByteArray &mimeType, const QByteArray &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)
| QHttpServerResponse(QHttpServerResponse &&other)
| ~QHttpServerResponse()
void | addHeader(const QByteArray &name, const QByteArray &value)
void | clearHeader(const QByteArray &name)
void | clearHeaders()
QByteArray | data() const
bool | hasHeader(const QByteArray &header) const
bool | hasHeader(const QByteArray &name, const QByteArray &value) const
`(since 6.8)` QHttpHeaders | headers() const
QList<QByteArray> | headers(const QByteArray &name) const
QByteArray | mimeType() const
void | setHeader(const QByteArray &name, const QByteArray &value)
QHttpServerResponse::StatusCode | statusCode() const
`(since 6.8)` QHttpServerResponse & | withHeaders(QHttpHeaders &&headers)
`(since 6.8)` QHttpServerResponse & | withHeaders(const QHttpHeaders &headers)
QHttpServerResponse & | operator=(QHttpServerResponse &&other)

## Static Public Members

QHttpServerResponse | fromFile(const QString &fileName)

## Detailed Description

API for creating, reading and modifying a response from an HTTP server, and for writing its contents to a QHttpServerResponder. It has numerous constructors, and `static` function `fromFile` for constructing it from the contents of a file. There are functions for setting, getting, and removing headers, and for getting the data, status code and mime type.

## Member Type Documentation

### `[alias]` QHttpServerResponse::StatusCode

Type alias for QHttpServerResponder::StatusCode

## Member Function Documentation

### QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, QByteArray &&data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

### QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, const QByteArray &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

Creates a QHttpServer response.

The response will use the given status code and deliver the data as its body, with a `ContentType` header describing it as being of MIME type mimeType.

### QHttpServerResponse::QHttpServerResponse(const QHttpServerResponse::StatusCode statusCode)

Creates a QHttpServerResponse object with the status code statusCode.

### `[explicit]` QHttpServerResponse::QHttpServerResponse(QByteArray &&data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

Move-constructs a QHttpServerResponse whose body will contain the given data with the status code status.

### `[explicit]` QHttpServerResponse::QHttpServerResponse(const QByteArray &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

Creates a QHttpServerResponse object from data with the status code status.

### QHttpServerResponse::QHttpServerResponse(const QJsonArray &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

Creates a QHttpServerResponse object from data with the status code status.

### QHttpServerResponse::QHttpServerResponse(const QJsonObject &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

Creates a QHttpServerResponse object from data with the status code status.

### QHttpServerResponse::QHttpServerResponse(const QString &data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

Creates a QHttpServerResponse object from data with the status code status.

### QHttpServerResponse::QHttpServerResponse(const char *data, const QHttpServerResponse::StatusCode status = StatusCode::Ok)

Creates a QHttpServerResponse object from data with the status code status.

### `[noexcept]` QHttpServerResponse::QHttpServerResponse(QHttpServerResponse &&other)

Move-constructs an instance of a QHttpServerResponse object from other.

### `[noexcept]` QHttpServerResponse::~QHttpServerResponse()

Destroys a QHttpServerResponse object.

### void QHttpServerResponse::addHeader(const QByteArray &name, const QByteArray &value)

Adds the HTTP header with name name and value value, does not override any previously set headers.

### void QHttpServerResponse::clearHeader(const QByteArray &name)

Removes the HTTP header with name name.

### void QHttpServerResponse::clearHeaders()

Removes all HTTP headers.

### QByteArray QHttpServerResponse::data() const

Returns the response body.

### `[static]` QHttpServerResponse QHttpServerResponse::fromFile(const QString &fileName)

Returns a QHttpServerResponse from the content of the file fileName.

It is the caller's responsibility to sanity-check the filename, and to have a well-defined policy for which files the server will request.

### bool QHttpServerResponse::hasHeader(const QByteArray &header) const

Returns true if the response contains an HTTP header with name header, otherwise returns false.

### bool QHttpServerResponse::hasHeader(const QByteArray &name, const QByteArray &value) const

Returns true if the response contains an HTTP header with name name and with value value, otherwise returns false.

### `[since 6.8]` QHttpHeaders QHttpServerResponse::headers() const

Returns the currently set HTTP headers.

This function was introduced in Qt 6.8.

### QList<QByteArray> QHttpServerResponse::headers(const QByteArray &name) const

Returns values of the HTTP header with name name.

### QByteArray QHttpServerResponse::mimeType() const

Returns the value of the HTTP "Content-Type" header.

Note: Default value is "text/html"

### void QHttpServerResponse::setHeader(const QByteArray &name, const QByteArray &value)

Sets the HTTP header with name name and value value, overriding any previously set headers.

See also hasHeader().

### QHttpServerResponse::StatusCode QHttpServerResponse::statusCode() const

Returns the status code.

### `[since 6.8]` QHttpServerResponse &QHttpServerResponse::withHeaders(QHttpHeaders &&headers)

Sets headers as the HTTP headers, overriding any previously set headers. Returns a reference to this object.

This function was introduced in Qt 6.8.

### `[since 6.8]` QHttpServerResponse &QHttpServerResponse::withHeaders(const QHttpHeaders &headers)

Sets headers as the HTTP headers, overriding any previously set headers. Returns a reference to this object.

This function was introduced in Qt 6.8.

### `[noexcept]` QHttpServerResponse &QHttpServerResponse::operator=(QHttpServerResponse &&other)

Move-assigns the values of other to this object.