---
title: "ClikaRT::http::ServerResponse"
sidebar_label: "ServerResponse"
description: "A response a handler returns. Build with a factory and chain with_header / with_cookie. Move-only."
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--class">class</span>

**Header:** `ClikaRT/http/server.h`

A response a handler returns. Build with a factory and chain [`with_header`](./ServerResponse.md#with_header) / [`with_cookie`](./ServerResponse.md#with_cookie). Move-only.

## Static member functions

### text() {#text}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`text`**(`std::string_view` `body`, `int` `status` `=` `200`)

</div>

A `text/plain` response carrying `body` at `status`.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 171

</div>

### json() {#json}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`json`**(`std::string_view` `body`, `int` `status` `=` `200`)

</div>

An `application/json` response carrying `body` at `status`.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 173

</div>

### html() {#html}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`html`**(`std::string_view` `body`, `int` `status` `=` `200`)

</div>

A `text/html` response carrying `body` at `status`.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 175

</div>

### bytes() {#bytes}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`bytes`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::string_view` `body`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::string_view` `content_type`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int` `status` `=` `200`<br />
)

</div>

Raw bytes with an explicit `content_type` at `status`.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 177

</div>

### error() {#error}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`error`**(`int` `status`)

</div>

status-only (page via the error handler)

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 179

</div>

### redirect() {#redirect}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`redirect`**(`std::string_view` `location`, `int` `status` `=` `302`)

</div>

`location` is emitted verbatim; validate it (same-origin) before building it from request input, or it becomes an open redirect. Redirect to `location` at `status` (default 302 Found).

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 183

</div>

### file() {#file}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`file`**(`std::string_view` `path`)

</div>

Serve a file (MIME inferred from the extension). `path` is caller-owned; contain it before deriving it from untrusted input (path traversal).

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 186

</div>

### stream() {#stream}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`stream`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::string_view` `content_type`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::function<`[`Result`](../Result.md)`<std::optional<std::string>>()>` `next`<br />
)

</div>

A chunked streaming body: `next` is pulled until it returns `nullopt` (done). It runs on a pool worker; don't block forever, and don't capture the [`ServerRequest`](./ServerRequest.md). A throw / failure [`Result`](../Result.md) aborts the stream (logged at `Warn` on [`ClikaRT::logging::logger()`](../logging/index.md#logger); headers are already out, so the log line is the only failure signal). An empty `next` answers with the server's 500 page, and one [`Error`](../Error.md) line on that logger names the cause.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 194

</div>

### sse() {#sse}

<div class="api-signature">

`static` [`ServerResponse`](./ServerResponse.md) **`sse`**(`std::function<`[`Result`](../Result.md)`<std::optional<`[`ServerSentEvent`](./ServerSentEvent.md)`>>()>` `next`)

</div>

A Server-Sent-Events body: `next` yields one event per call, `nullopt` ⇒ done. Same worker / non-blocking rules as [`stream`](./ServerResponse.md#stream), and the same answer to an empty `next`.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 199

</div>

## Member functions

### ServerResponse() {#ServerResponse}

<div class="api-signature">

**`ServerResponse`**(`ServerResponse&&`) `noexcept`

</div>

Move-only: transfers the response.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 163

</div>

### operator=() {#operator-assign}

<div class="api-signature">

[`ServerResponse`](./ServerResponse.md)`&` **`operator=`**([`ServerResponse`](./ServerResponse.md)`&&`) `noexcept`

</div>

Move-assign: transfers the response.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 165

</div>

### \~ServerResponse() {#ServerResponse-2}

<div class="api-signature">

**`~ServerResponse`**()

</div>

Releases the response.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 167

</div>

### with\_header() {#with_header}

<div class="api-signature">

[`ServerResponse`](./ServerResponse.md)`&` **`with_header`**(`std::string_view` `name`, `std::string_view` `value`) `&`

</div>

Append a header (chainable). Multiple values for one name are allowed.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 202

</div>

### with\_cookie() {#with_cookie}

<div class="api-signature">

[`ServerResponse`](./ServerResponse.md)`&` **`with_cookie`**([`Cookie`](./Cookie.md) `cookie`) `&`

</div>

Set a cookie (chainable); formats a Set-Cookie header.

<div class="api-declared">

Declared in `ClikaRT/http/server.h`, line 204

</div>
