Lambdas behind API Gateways need to send specially formatted responses that look like this:

{
  "statusCode": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "isBase64Encoded": false,
  "body": "{\"best_animal\": \"corgi\"}"
}

For basic applications where the handler function is returning a simple result, lambdr will do its best to automatically return a result compatible with API Gateways. It will do this whenever an event is detected as having come via an API Gateway. For most purposes this is sufficient, and allows users to focus on the handler function rather than the specifics of how AWS Lambda works.

For more complicated applications, such as when the Lambda needs to return a specific content type or specific headers, may require a bespoke response. This function will take any R object and format it in style of the above example, allowing for customisation.

When the handler function returns a html_response the formatted result will be returned to the API Gateway without further serialisation.

html_response(
  body,
  is_base64 = FALSE,
  status_code = 200L,
  content_type = NULL,
  headers = list()
)

Arguments

body

the actual result to be delivered. This is not serialised in any way, so if this is a list to be interpreted JSON it should be stringified, that is, it should be a string of a JSON. Consider using the as_stringified_json function.

is_base64

logical which indicates if body is Base64 encoded. Defaults to False.

status_code

intger status code of the response. Defaults to 200L (OK).

content_type

MIME type for the content. This will be appended to the headers (as "Content-Type"), unless such a value is already provided to headers, in which case this argument is ignored. If not provided then no information on headers will be sent in the response, leaving the beahviour up to the defaults of the API Gateway.

headers

additional headers, as a named list, to be included in the response. If this contains a "Content-Type" value then content_type is ignored.

Value

A stringified JSON response for an API Gateway, with the "already_serialised" attribute marked as TRUE. This will stop serialise_result from attempting to serialise the result again.

Examples

html_response("abc")
#> [1] "{\"body\":\"abc\",\"isBase64Encoded\":false,\"statusCode\":200}"
#> attr(,"already_serialised")
#> [1] TRUE
html_response("YWJj", is_base64 = TRUE)
#> [1] "{\"body\":\"YWJj\",\"isBase64Encoded\":true,\"statusCode\":200}"
#> attr(,"already_serialised")
#> [1] TRUE
html_response("abc", headers = list(x = "a"))
#> [1] "{\"body\":\"abc\",\"isBase64Encoded\":false,\"statusCode\":200,\"headers\":{\"x\":\"a\"}}"
#> attr(,"already_serialised")
#> [1] TRUE
html_response(
  "<html><body>Hello World!</body></html>",
  content_type = "text/html"
)
#> [1] "{\"body\":\"<html><body>Hello World!<\\/body><\\/html>\",\"isBase64Encoded\":false,\"statusCode\":200,\"headers\":{\"Content-Type\":\"text/html\"}}"
#> attr(,"already_serialised")
#> [1] TRUE