A `Response` object represents the HTTP response that a `Beakr` app sends when handling a request. By convention, the response object is named `res` (with the corresponding request named `req`).

Format

An [`R6::R6Class`] generator for `Response` objects.

See also

[Router], [Request], [Error]

Public fields

headers

A named list of HTTP response headers. Defaults to `list("Content-Type" = "text/html")`.

status

An integer HTTP status code. Defaults to `200L`.

body

The response body. May be `NULL`, character, raw, JSON, or base64.

Methods


Method setHeader()

Set a header key-value pair (e.g., `"Content-Type" = "text/html"`).

Usage

Response$setHeader(key, value)

Arguments

key

Header name.

value

Header value.


Method setContentType()

Set the response `Content-Type`.

Usage

Response$setContentType(type)

Arguments

type

MIME type string.


Method setStatus()

Set the HTTP status code.

Usage

Response$setStatus(status)

Arguments

status

Integer HTTP status code.


Method setBody()

Set the response body, respecting the current `Content-Type`.

Usage

Response$setBody(body)

Arguments

body

Body content, type depends on `Content-Type`.


Method redirect()

Redirect the client by setting status 302 and `Location` header.

Usage

Response$redirect(url)

Arguments

url

The URL to redirect to.


Method json()

Convert `txt` to JSON and set content type to `"application/json"`.

Usage

Response$json(txt, auto_unbox = TRUE)

Arguments

txt

Content to convert to JSON.

auto_unbox

Logical; whether to simplify length-1 vectors.


Method text()

Set the response body as plain text and content type `"text/html"`.

Usage

Response$text(txt)

Arguments

txt

Content to include as plain text.


Method structured()

Return a structured response depending on `protocol`.

Usage

Response$structured(protocol)

Arguments

protocol

`"http"` or `"websocket"`.


Method plot()

Render a plot to PNG (optionally base64-encode) and set as response body.

Usage

Response$plot(plot_object, base64 = TRUE, ...)

Arguments

plot_object

A plot object to render.

base64

Logical; if `TRUE`, encode image as base64.

...

Passed to [graphics::png()].


Method clone()

The objects of this class are cloneable with this method.

Usage

Response$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.