R/mw-raw.R

Defines functions mw_raw

Documented in mw_raw

#' Middleware to read the raw body of a request
#'
#' Adds the raw body, as a raw object to the `raw` field of the request.
#'
#' @param type Content type to match. If it does not match, then the
#' request object is not modified.
#' @return Handler function.
#'
#' @family middleware
#' @export
#' @examples
#' app <- new_app()
#' app$use(mw_raw())
#' app

mw_raw <- function(type = "application/octet-stream") {
  function(req, res) {
    ct <- req$get_header("Content-Type") %||% ""
    if (! ct %in% tolower(type)) return("next")
    req$raw <- req$.body
    "next"
  }
}

Try the webfakes package in your browser

Any scripts or data that you put into this service are public.

webfakes documentation built on Oct. 1, 2023, 9:06 a.m.