Response | R Documentation |
Creates response object.
body
Response body.
If it is a named character with a name file
or tmpfile
then the value is considered as a path to a file and content oh this file
is served as body. The latter will be deleted once served.
content_type
Response body content (media) type. Will be translated
to Content-type
header.
headers
Response headers.
status_code
Response HTTP status code.
cookies
Response cookies. Will be translated to Set-Cookie
headers.
context
Environment to store any data. Can be used in middlewares.
encode
Function to encode body for specific content.
status
Paste together status code and description.
new()
Creates Response object
Response$new( body = NULL, content_type = "text/plain", headers = list(Server = getOption("RestRserve.headers.server")), status_code = 200L, encode = NULL, ... )
body
Response body.
content_type
Response body content (media) type.
headers
Response headers.
status_code
Response status code.
encode
Function to encode body for specific content.
...
Not used at this moment.
reset()
Resets response object. This is not useful for end user, but useful for RestRserve internals - resetting R6 class is much faster then initialize it.
Response$reset()
set_content_type()
Set content type for response body.
Response$set_content_type(content_type = "text/plain")
content_type
Response body content (media) type.
set_status_code()
Set HTTP status code for response. See docs on MDN.
Response$set_status_code(code)
code
Status code as integer number.
has_header()
Determine whether or not the response header exists.
Response$has_header(name)
name
Header field name.
Logical value.
get_header()
Get HTTP response header value. If requested header is empty returns default
.
Response$get_header(name, default = NULL)
name
Header field name.
default
Default value if header does not exists.
Header field values (character string).
set_header()
Set HTTP response header. Content-type
and Content-length
headers not
allowed (use content_type
field instead).
Response$set_header(name, value)
name
Header field name.
value
Header field value.
delete_header()
Unset HTTP response header.
Response$delete_header(name)
name
Header field name.
Logical value.
append_header()
Append HTTP response header. If header exists ,
separator will be used.
Don't use this method to set cookie (use set_cookie
method instead).
Response$append_header(name, value)
name
Header field name.
value
Header field value.
set_date()
Set Date
HTTP header. See docs on MDN.
Response$set_date(dtm = Sys.time())
dtm
POSIXct value.
unset_date()
Unset Date
HTTP header.
Response$unset_date()
Logical value.
set_cookie()
Set cookie. See docs on MDN.
Response$set_cookie( name, value, expires = NULL, max_age = NULL, domain = NULL, path = NULL, secure = NULL, http_only = NULL )
name
Cookie name.
value
Cookie value.
expires
Cookie expires date and time (POSIXct).
max_age
Max cookie age (integer).
domain
Cookie domain.
path
Cookie path.
secure
Cookie secure flag.
http_only
Cookie HTTP only flag.
unset_cookie()
Unset cookie with given name.
Response$unset_cookie(name)
name
Cookie name.
Logical value.
set_body()
Set response body.
Response$set_body(body)
body
Response body.
set_response()
Set response fields.
Response$set_response( status_code, body = NULL, content_type = self$content_type )
status_code
Response HTTP status code.
body
Response body.
content_type
content_type Response body content (media) type.
print()
Print method.
Response$print()
clone()
The objects of this class are cloneable with this method.
Response$clone(deep = FALSE)
deep
Whether to make a deep clone.
Request Application
# init response
rs = Response$new()
# set body media type
rs$set_content_type("text/plain")
# set body content
rs$set_body("OK")
# set response status code
rs$set_status_code(200L)
# print response
rs
# init response
rs = Response$new()
# static file path
file_path = system.file("DESCRIPTION", package = "RestRserve")
# get last file modification timestamp
file_mtime = file.mtime(file_path)
# set body
rs$set_body(c("file" = file_path))
# set content type
rs$set_content_type("text/plain")
# set current timestamp
rs$set_date()
# set 'last-modified' header
rs$set_header("Last-Modified", as(file_mtime, "HTTPDate"))
# print response
rs
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.