io.pedestal.http.body-params
Provides interceptors and support for parsing the body of a request, generally according to the content type header. This results in new keys on the request map, depending on the type of data parsed.
add-parser
(add-parser parser-map content-type parser-fn)
Adds a parser to the parser map; content type can either be a string (to exactly match the content type), or a regular expression.
The parser-fn is passed the request map and returns a modified request map with an appropriate key and parsed value. For example, the default JSON parser adds a :json-params key.
add-ring-middleware
deprecated in 0.7.0
(add-ring-middleware parser-map content-type middleware)
body-params
(body-params)
(body-params parser-map)
Returns an interceptor that will parse the body of the request according to the content type. The normal rules are provided by default-parser-map which maps a regular expression identifying a content type to a parsing function for that type.
Parameters parsed from the body are added as a new key to the request map dependending on which parser does the work; for the default parsers, one of the following will be used:
- :json-params
- :edn-params
- :form-params
- :transit-params
custom-edn-parser
(custom-edn-parser & options)
Return an edn-parser fn that, given a request, will read the body of that request with edn/read
. options are key-val pairs that will be passed as a hash-map to edn/read
.
custom-json-parser
(custom-json-parser & options)
Return a json-parser fn that, given a request, will read the body of the request with json/parse-stream
. options are key-val pairs that will be passed along to json/parse-stream
.
custom-transit-parser
(custom-transit-parser & options)
Return a transit-parser fn that, given a request, will read the body of that request with transit/read
. options is a sequence to pass to transit/reader along with the body of the request.
default-parser-map
(default-parser-map & parser-options)
Return a map of MIME-type to parsers. Included types are edn, json and form-encoding. parser-options are key-val pairs, valid options are:
:edn-options A hash-map of options to be used when invoking edn/read
. :json-options A hash-map of options to be used when invoking json/parse-stream
. :transit-options A vector of options to be used when invoking transit/reader
- must apply to both json and msgpack
Examples:
(default-parser-map :json-options {:key-fn keyword}) ;; This parser-map would parse the json body ‘{“foo”: “bar”}’ as ;; {:foo “bar”}
(default-parser-map :edn-options {:readers data-readers}) ;; This parser-map will parse edn bodies using any custom edn readers you ;; define (in a data_readers.clj file, for example.)
(default-parser-map :transit-options {:handlers {“custom/model” custom-model-read-handler}}) ;; This parser-map will parse the transit body using a handler defined by ;; custom-model-read-handler.
edn-parser
deprecated in 0.7.0
(edn-parser & args)
Take a request and parse its body as edn.
Invoke custom-edn-parser instead.
json-parser
deprecated in 0.7.0
(json-parser & args)
Take a request and parse its body as JSON.
Use custom-json-parser instead.
transit-parser
deprecated in 0.7.0
(transit-parser & args)
Take a request and parse its body as JSON transit.
Use custom-transit-parser instead.