plumber (version 1.0.0)

register_parser: Manage parsers

Description

A parser is responsible for decoding the raw body content of a request into a list of arguments that can be mapped to endpoint function arguments. For instance, parser_json() parse content-type application/json.

Usage

register_parser(alias, parser, fixed = NULL, regex = NULL, verbose = TRUE)

registered_parsers()

Arguments

alias

An alias to map parser from the @parser plumber tag to the global parsers list.

parser

The parser function to be added. This build the parser function. See Details for more information.

fixed

A character vector of fixed string to be matched against a request content-type to use parser.

regex

A character vector of regex string to be matched against a request content-type to use parser.

verbose

Logical value which determines if a warning should be displayed when alias in map are overwritten.

Functions

  • registered_parsers: Return all registered parsers

Details

When parser is evaluated, it should return a parser function. Parser matching is done first by content-type header matching on fixed then by using a regular expressions on regex. Note that plumber strip the header from ; charset* to perform matching.

There is a special case when no content-type header is provided that will use a parser_json() when it detects a json string.

Functions signature should include value, ... and possibly content_type, filename. Other parameters may be provided if you want to use the headers from webutils::parse_multipart().

Parser function structure is something like below.

parser <- function(parser_arguments_here) {
  # return a function to parse a raw value
  function(value, ...) {
    # do something with raw value
  }
}

Examples

Run this code
# NOT RUN {
# `content-type` header is mostly used to look up charset and adjust encoding
parser_dcf <- function(...) {
  function(value, content_type = "text/x-dcf", ...) {
    charset <- get_character_set(content_type)
    value <- rawToChar(value)
    Encoding(value) <- charset
    read.dcf(value, ...)
  }
}

# Could also leverage existing parsers
parser_dcf <- function(...) {
  parser_read_file(function(tmpfile) {
    read.dcf(tmpfile, ...)
  })
}

# Register the newly created parser
# }
# NOT RUN {
register_parser("dcf", parser_dcf, fixed = "text/x-dcf")
# }

Run the code above in your browser using DataLab