Learn R Programming

RestRserve (version 0.2.0.2)

Response: Creates response object

Description

Creates response object.

Arguments

Format

R6::R6Class object.

Construction

Response$new(body = "",
             content_type = 'text/plain',
             headers = structure(list(), names = character(0)),
             status_code = 200L,
             encode = NULL)

Fields

  • content_type :: character(1) Response body content (media) type. Will be translated to Content-type header.

  • body :: raw() | character(1) 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.

  • status_code :: integer(1) Response status code.

  • headers :: named list() Response headers.

  • cookies :: named list() Response cookies. Will be translated to Set-Cookie headers.

  • context :: environment() Environment to store any data. Can be used in middlewares.

  • encode :: function unction to encode body for specific content

  • status :: character(1) Paste together status code and description.

Methods

  • set_content_type(content_type = 'text/plain') character(1) -> self Set content type for response body.

  • set_status_code(code) integer(1) -> self Set status code for response. See docs on MDN.

  • has_header(name) character(1) -> logical(1) Determine whether or not the response header exists.

  • get_header(name, default = NULL) character(1), character(1) -> character() Get HTTP response header value. If requested header is empty returns default.

  • set_header(name, value) character(1), character() -> self Set HTTP response header. Content-type and Content-length headers not allowed (use content_type field instead).

  • append_header(name, value) character(1), character() -> self Append HTTP response header. If header exists , separator will be used. Don't use this method to set cookie (use set_cookie method instead).

  • delete_header(name) character(1) -> logical(1) Unset HTTP response header.

  • set_cookie(name, value, expires = NULL, max_age = NULL, domain = NULL, path = NULL, secure = NULL, http_only = NULL) character(1), character(1), POSIXct(1), integer(1), character(1), character(1), logical(1), logical(1) -> self Set cookie. See docs on MDN.

  • unset_cookie(name) character(1) -> logical(1) Unset cookie with given name.

  • set_date(dtm = Sys.time()) POSIXct(1) -> self Set Date HTTP header. See docs on MDN.

  • unset_date() -> logical(1) Unset Date HTTP header.

  • set_body(body) any -> self Set response body.

  • set_response(status_code, body = NULL, content_type = self$content_type) integer(1), any, character(1) -> self Set response fields.

See Also

Request Application

Examples

Run this code
# NOT RUN {
# 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

# }

Run the code above in your browser using DataLab