If your WebQueue's handler
function returns a list, json object, character
vector, or scalar integer, response()
will be used to transform that
result into an HTTP response.
You may also call response()
within your handler to better customize the
HTTP response. Or, return a result of class 'AsIs' to have that object
passed directly on to 'httpuv'.
Arguments
- body
The content. A list will be encoded as JSON. A scalar integer will be interpreted as a status. A character vector will be concatenated with no separator.
- status
A HTTP response status code.
- headers
A named character vector of HTTP headers. A list-like object is acceptable if all elements are simple strings.
- ...
Objects created by
header()
and/orcookie()
. Or key-value pairs to add toheaders
.
Value
A <response/AsIs>
object. Essentially a list with elements named
body
, status
, and headers
formatted as 'httpuv' expects.
Examples
library(webqueue)
response(list(name = unbox('Peter'), pi = pi))
#> HTTP/1.1 200 OK
#> Content-Type: application/json; charset=utf-8
#>
#> {"name":"Peter","pi":[3.1416]}
response(307L, Location = '/new/file.html')
#> HTTP/1.1 307 Temporary Redirect
#> Location: /new/file.html
#>
#> Temporary Redirect
# The `body` and `status` slots also handle header objects.
response(cookie(id = 123, http_only = TRUE))
#> HTTP/1.1 200 OK
#> Set-Cookie: id=123; HttpOnly
#>
#> OK
# Allow javascript to access custom headers.
uid <- header('x-user-id' = 100, expose = TRUE)
sid <- header('x-session-id' = 303, expose = TRUE)
response(uid, sid)
#> HTTP/1.1 200 OK
#> x-user-id: 100
#> x-session-id: 303
#> Access-Control-Expose-Headers: x-user-id, x-session-id
#>
#> OK