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.
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.
set_status_code()
Set HTTP status code for response. See docs on MDN.
set_header()
Set HTTP response header. Content-type
and Content-length
headers not
allowed (use content_type
field instead).
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).
set_cookie()
Set cookie. See docs on MDN.
# 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
#> <RestRserve Response>
#> status code: 200 OK
#> content-type: text/plain
#> <Headers>
#> Server: RestRserve/1.2.2; Rserve/1.8.13
# 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
#> <RestRserve Response>
#> status code: 200 OK
#> content-type: text/plain
#> <Headers>
#> Server: RestRserve/1.2.2; Rserve/1.8.13
#> Date: Thu, 18 Apr 2024 01:55:10 GMT
#> Last-Modified: Thu, 18 Apr 2024 01:53:21 GMT