Learn R Programming

webqueue (version 1.0.0)

response: Compile an HTTP response.

Description

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'.

Usage

response(body = NULL, status = 200L, headers = NULL, ...)

Value

A <response/AsIs> object. Essentially a list with elements named body, status, and headers formatted as 'httpuv' expects.

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/or cookie(). Or key-value pairs to add to headers.

Examples

Run this code

    library(webqueue)
    
    response(list(name = unbox('Peter'), pi = pi))
         
    response(307L, Location = '/new/file.html')
    
    # The `body` and `status` slots also handle header objects.
    response(cookie(id = 123, http_only = TRUE))
    
    # 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)
    

Run the code above in your browser using DataLab