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, the parser_json parser content-type application/json. The list of available parsers in plumber is global.

addParser(name, parser, pattern = NULL)

## Arguments

name The name of the parser (character string) The parser to be added. A pattern to match against the content-type of each part of the request body.

## Details

For instance, the parser_json pattern is application/json. If pattern is not provided, will be set to application/{name}. Detection is done assuming content-type starts with pattern and is case sensitive.

Parser function structure is something like below. Available parameters to build parser are value, content_type and filename (only available in multipart-form body).

parser <- function() {
function(value, content_type = "ct", filename, ...) {
# do something with raw value
}
}

It should return a named list if you want values to map to plumber endpoint function args.

## Examples

parser_json <- function() {
function(value, content_type = "application/json", ...) {
charset <- getCharacterSet(content_type)
value <- rawToChar(value)
Encoding(value) <- charset
jsonlite::fromJSON(value)
}
}