| Response {RestRserve} | R Documentation |
Creates Response object
Description
Creates response object.
Public fields
bodyResponse body.
If it is a named character with a namefileortmpfilethen 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_typeResponse body content (media) type. Will be translated to
Content-typeheader.headersResponse headers.
status_codeResponse HTTP status code.
cookiesResponse cookies. Will be translated to
Set-Cookieheaders.contextEnvironment to store any data. Can be used in middlewares.
encodeFunction to encode body for specific content.
Active bindings
statusPaste together status code and description.
Methods
Public methods
Method new()
Creates Response object
Usage
Response$new(
body = NULL,
content_type = "text/plain",
headers = list(Server = getOption("RestRserve.headers.server")),
status_code = 200L,
encode = NULL,
...
)Arguments
bodyResponse body.
content_typeResponse body content (media) type.
headersResponse headers.
status_codeResponse status code.
encodeFunction to encode body for specific content.
...Not used at this moment.
Method 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.
Usage
Response$reset()
Method set_content_type()
Set content type for response body.
Usage
Response$set_content_type(content_type = "text/plain")
Arguments
content_typeResponse body content (media) type.
Method set_status_code()
Set HTTP status code for response. See docs on MDN.
Usage
Response$set_status_code(code)
Arguments
codeStatus code as integer number.
Method has_header()
Determine whether or not the response header exists.
Usage
Response$has_header(name)
Arguments
nameHeader field name.
Returns
Logical value.
Method get_header()
Get HTTP response header value. If requested header is empty returns default.
Usage
Response$get_header(name, default = NULL)
Arguments
nameHeader field name.
defaultDefault value if header does not exists.
Returns
Header field values (character string).
Method set_header()
Set HTTP response header. Content-type and Content-length headers not
allowed (use content_type field instead).
Usage
Response$set_header(name, value)
Arguments
nameHeader field name.
valueHeader field value.
Method delete_header()
Unset HTTP response header.
Usage
Response$delete_header(name)
Arguments
nameHeader field name.
Returns
Logical value.
Method 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).
Usage
Response$append_header(name, value)
Arguments
nameHeader field name.
valueHeader field value.
Method set_date()
Set Date HTTP header. See docs on MDN.
Usage
Response$set_date(dtm = Sys.time())
Arguments
dtmPOSIXct value.
Method unset_date()
Unset Date HTTP header.
Usage
Response$unset_date()
Returns
Logical value.
Method set_cookie()
Set cookie. See docs on MDN.
Usage
Response$set_cookie( name, value, expires = NULL, max_age = NULL, domain = NULL, path = NULL, secure = NULL, http_only = NULL )
Arguments
nameCookie name.
valueCookie value.
expiresCookie expires date and time (POSIXct).
max_ageMax cookie age (integer).
domainCookie domain.
pathCookie path.
secureCookie secure flag.
http_onlyCookie HTTP only flag.
Method unset_cookie()
Unset cookie with given name.
Usage
Response$unset_cookie(name)
Arguments
nameCookie name.
Returns
Logical value.
Method set_body()
Set response body.
Usage
Response$set_body(body)
Arguments
bodyResponse body.
Method set_response()
Set response fields.
Usage
Response$set_response( status_code, body = NULL, content_type = self$content_type )
Arguments
status_codeResponse HTTP status code.
bodyResponse body.
content_typecontent_type Response body content (media) type.
Method print()
Print method.
Usage
Response$print()
Method clone()
The objects of this class are cloneable with this method.
Usage
Response$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Examples
# 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