Creates response object.

See also

Public fields

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.

Active bindings

status

Paste together status code and description.

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

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.


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_type

Response 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

code

Status code as integer number.


Method has_header()

Determine whether or not the response header exists.

Usage

Response$has_header(name)

Arguments

name

Header 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

name

Header field name.

default

Default 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

name

Header field name.

value

Header field value.


Method delete_header()

Unset HTTP response header.

Usage

Response$delete_header(name)

Arguments

name

Header 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

name

Header field name.

value

Header field value.


Method set_date()

Set Date HTTP header. See docs on MDN.

Usage

Response$set_date(dtm = Sys.time())

Arguments

dtm

POSIXct value.


Method unset_date()

Unset Date HTTP header.

Usage

Response$unset_date()

Returns

Logical value.


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

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 with given name.

Usage

Response$unset_cookie(name)

Arguments

name

Cookie name.

Returns

Logical value.


Method set_body()

Set response body.

Usage

Response$set_body(body)

Arguments

body

Response body.


Method set_response()

Set response fields.

Usage

Response$set_response(
  status_code,
  body = NULL,
  content_type = self$content_type
)

Arguments

status_code

Response HTTP status code.

body

Response body.

content_type

content_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

deep

Whether to make a deep clone.

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
#> <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