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`).
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
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)
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`.
Arguments
body
Body content, type depends on `Content-Type`.
Method redirect()
Redirect the client by setting status 302 and `Location` header.
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.
Set the response body as plain text and content type `"text/html"`.
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"`.
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.