Response {ambiorix} | R Documentation |
Response
Description
Response class to generate responses sent from the server.
Value
A Response object.
Active bindings
status
Status of the response, defaults to
200L
.headers
Named list of headers.
Methods
Public methods
Method set_status()
Usage
Response$set_status(status)
Arguments
status
An integer defining the status.
Details
Set the status of the response.
Method send()
Usage
Response$send(body, headers = NULL, status = NULL)
Arguments
body
Body of the response.
headers
HTTP headers to set.
status
Status of the response, if
NULL
usesself$status
.
Details
Send a plain HTML response.
Method sendf()
Usage
Response$sendf(body, ..., headers = NULL, status = NULL)
Arguments
body
Body of the response.
...
Passed to
...
ofsprintf
.headers
HTTP headers to set.
status
Status of the response, if
NULL
usesself$status
.
Details
Send a plain HTML response, pre-processed with sprintf.
Method text()
Usage
Response$text(body, headers = NULL, status = NULL)
Arguments
body
Body of the response.
headers
HTTP headers to set.
status
Status of the response, if
NULL
usesself$status
.
Details
Send a plain text response.
Method send_file()
Usage
Response$send_file(file, headers = NULL, status = NULL)
Arguments
file
File to send.
headers
HTTP headers to set.
status
Status of the response.
Details
Send a file.
Method redirect()
Usage
Response$redirect(path, status = NULL)
Arguments
path
Path or URL to redirect to.
status
Status of the response, if
NULL
usesself$status
.
Details
Redirect to a path or URL.
Method render()
Usage
Response$render(file, data = list(), headers = NULL, status = NULL)
Arguments
file
Template file.
data
List to fill
[% tags %]
.headers
HTTP headers to set.
status
Status of the response, if
NULL
usesself$status
.
Details
Render a template file.
Method json()
Usage
Response$json(body, headers = NULL, status = NULL, ...)
Arguments
body
Body of the response.
headers
HTTP headers to set.
status
Status of the response, if
NULL
usesself$status
....
Additional named arguments passed to the serialiser.
Details
Render an object as JSON.
Method csv()
Usage
Response$csv(data, name = "data", status = NULL, ...)
Arguments
data
Data to convert to CSV.
name
Name of the file.
status
Status of the response, if
NULL
usesself$status
....
Additional arguments passed to
readr::format_csv()
.
Details
Sends a comma separated value file
Method tsv()
Usage
Response$tsv(data, name = "data", status = NULL, ...)
Arguments
data
Data to convert to CSV.
name
Name of the file.
status
Status of the response, if
NULL
usesself$status
....
Additional arguments passed to
readr::format_tsv()
.
Details
Sends a tab separated value file
Method htmlwidget()
Usage
Response$htmlwidget(widget, status = NULL, ...)
Arguments
widget
The widget to use.
status
Status of the response, if
NULL
usesself$status
....
Additional arguments passed to
htmlwidgets::saveWidget()
.
Details
Sends an htmlwidget.
Method md()
Usage
Response$md(file, data = list(), headers = NULL, status = NULL)
Arguments
file
Template file.
data
List to fill
[% tags %]
.headers
HTTP headers to set.
status
Status of the response, if
NULL
usesself$status
.
Details
Render a markdown file.
Method png()
Usage
Response$png(file)
Arguments
file
Path to local file.
Details
Send a png file
Method jpeg()
Usage
Response$jpeg(file)
Arguments
file
Path to local file.
Details
Send a jpeg file
Method image()
Usage
Response$image(file)
Arguments
file
Path to local file.
Details
Send an image
Similar to png
and jpeg
methods but guesses correct method
based on file extension.
Method ggplot2()
Usage
Response$ggplot2(plot, ..., type = c("png", "jpeg"))
Arguments
plot
Ggplot2 plot object.
...
Passed to
ggplot2::ggsave()
type
Type of image to save.
Details
Ggplot2
Method print()
Usage
Response$print()
Details
Method set()
Usage
Response$set(name, value)
Arguments
name
String. Name of the variable.
value
Value of the variable.
Details
Set Data
Returns
Invisible returns self.
Method get()
Usage
Response$get(name)
Arguments
name
String. Name of the variable to get.
Details
Get data
Method header()
Usage
Response$header(name, value)
Arguments
name
String. Name of the header.
value
Value of the header.
Details
Add headers to the response.
Returns
Invisibly returns self.
Method header_content_json()
Usage
Response$header_content_json()
Details
Set Content Type to JSON
Returns
Invisibly returns self.
Method header_content_html()
Usage
Response$header_content_html()
Details
Set Content Type to HTML
Returns
Invisibly returns self.
Method header_content_plain()
Usage
Response$header_content_plain()
Details
Set Content Type to Plain Text
Returns
Invisibly returns self.
Method header_content_csv()
Usage
Response$header_content_csv()
Details
Set Content Type to CSV
Returns
Invisibly returns self.
Method header_content_tsv()
Usage
Response$header_content_tsv()
Details
Set Content Type to TSV
Returns
Invisibly returns self.
Method get_headers()
Usage
Response$get_headers()
Details
Get headers Returns the list of headers currently set.
Method get_header()
Usage
Response$get_header(name)
Arguments
name
Name of the header to return.
Details
Get a header
Returns a single header currently, NULL
if not set.
Method set_headers()
Usage
Response$set_headers(headers)
Arguments
headers
A named list of headers to set.
Details
Set headers
Method set_header()
Usage
Response$set_header(name, value)
Arguments
name
Name of the header.
value
Value to set.
Details
Set a Header
Returns
Invisible returns self.
Method pre_render_hook()
Usage
Response$pre_render_hook(hook)
Arguments
hook
A function that accepts at least 4 arguments:
-
self
: TheRequest
class instance. -
content
: File content a vector of character string, content of the template. -
data
:list
passed fromrender
method. -
ext
: File extension of the template file.
This function is used to add pre-render hooks to the
render
method. The function should return an object of classresponsePreHook
as obtained bypre_hook()
. This is meant to be used by middlewares to, if necessary, pre-process rendered data.Include
...
in yourhook
to ensure it will handle potential updates to hooks in the future.-
Details
Add a pre render hook.
Runs before the render
and send_file
method.
Returns
Invisible returns self.
Method post_render_hook()
Usage
Response$post_render_hook(hook)
Arguments
hook
A function to run after the rendering of HTML. It should accept at least 3 arguments:
-
self
: TheResponse
class instance. -
content
: File content a vector of character string, content of the template. -
ext
: File extension of the template file.
Include
...
in yourhook
to ensure it will handle potential updates to hooks in the future.-
Details
Post render hook.
Returns
Invisible returns self.
Method cookie()
Usage
Response$cookie( name, value, expires = getOption("ambiorix.cookie.expire"), max_age = getOption("ambiorix.cookie.maxage"), domain = getOption("ambiorix.cookie.domain"), path = getOption("ambiorix.cookie.path", "/"), secure = getOption("ambiorix.cookie.secure", TRUE), http_only = getOption("ambiorix.cookie.httponly", TRUE), same_site = getOption("ambiorix.cookie.savesite") )
Arguments
name
String. Name of the cookie.
value
value of the cookie.
expires
Expiry, if an integer assumes it's the number of seconds from now. Otherwise accepts an object of class
POSIXct
orDate
. If acharacter
string then it is set as-is and not pre-processed. If unspecified, the cookie becomes a session cookie. A session finishes when the client shuts down, after which the session cookie is removed.max_age
Indicates the number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both
expires
andmax_age
are set, the latter has precedence.domain
Defines the host to which the cookie will be sent. If omitted, this attribute defaults to the host of the current document URL, not including subdomains.
path
Indicates the path that must exist in the requested URL for the browser to send the Cookie header.
secure
Indicates that the cookie is sent to the server only when a request is made with the https: scheme (except on localhost), and therefore, is more resistant to man-in-the-middle attacks.
http_only
Forbids JavaScript from accessing the cookie, for example, through the document.cookie property.
same_site
Controls whether or not a cookie is sent with cross-origin requests, providing some protection against cross-site request forgery attacks (CSRF). Accepts
Strict
,Lax
, orNone
.
Details
Set a cookie
Overwrites existing cookie of the same name
.
Returns
Invisibly returns self.
Method clear_cookie()
Usage
Response$clear_cookie(name)
Arguments
name
Name of the cookie to clear.
Details
Clear a cookie Clears the value of a cookie.
Returns
Invisibly returns self.
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.
Examples
if (interactive()) {
library(ambiorix)
app <- Ambiorix$new()
app$get("/", function(req, res) {
# print(res)
res$send("Using {ambiorix}!")
})
app$start()
}