ring.middleware.format-response

available-charsets

Set of recognised charsets by the current JVM

can-encode?

(can-encode? {:keys [enc-type], :as encoder} {:keys [type sub-type], :as accepted-type})

Check whether encoder can encode to accepted-type. Accepted-type should have keys :type and :sub-type with appropriate values.

choose-charset

Memoized form of choose-charset*

choose-charset*

(choose-charset* accept-charset)

Returns an useful charset from the accept-charset string. Defaults to utf-8

default-charset-extractor

(default-charset-extractor request)

Default charset extractor, which returns either Accept-Charset header field or utf-8

default-handle-error

(default-handle-error e _ _)

Default error handling function used, which rethrows the Exception

make-encoder

(make-encoder encoder content-type binary?)(make-encoder encoder content-type)

Return a encoder map suitable for wrap-format-response. f takes a string and returns an encoded string type Content-Type of the encoded string (make-encoder json/generate-string “application/json”)

parse-accept-header

Memoized form of parse-accept-header*

parse-accept-header*

(parse-accept-header* accept-header)

Parse Accept headers into a sorted sequence of maps. “application/json;level=1;q=0.4” => ({:type “application” :sub-type “json” :q 0.4 :parameter “level=1”})

parse-charset-accepted

(parse-charset-accepted v)

Parses an accept-charset string to a list of [charset quality-score]

preferred-charset

(preferred-charset charsets)

Returns an acceptable choice from a list of [charset quality-score]

preferred-encoder

(preferred-encoder encoders req)

Return the encoder that encodes to the most preferred type. If the Accept header of the request is a String, assume it is according to Ring spec. Else assume the header is a sequence of accepted types sorted by their preference. If no accepted encoder is found, return nil. If no Accept header is found, return the first encoder.

wrap-clojure-response

(wrap-clojure-response handler & {:keys [predicate encoder type charset hf handle-error], :or {predicate serializable?, encoder generate-native-clojure, type "application/edn", charset default-charset-extractor, hf false, handle-error default-handle-error}})

Wrapper to serialize structures in :body to Clojure native with sane defaults. If :hf is set to true, will use print-dup for high-fidelity printing ( see here ). See wrap-format-response for more details.

wrap-format-response

(wrap-format-response handler & {:keys [predicate encoders charset binary? handle-error]})

Wraps a handler such that responses body to requests are formatted to the right format. If no Accept header is found, use the first encoder.

  • :predicate is a predicate taking the request and response as arguments to test if serialization should be used
  • :encoders a sequence of maps given by make-encoder
  • :charset can be either a string representing a valid charset or a fn taking the req as argument and returning a valid charset (*utf-8* is strongly suggested)
  • :binary? if true :charset will be ignored and decoder will receive an InputStream
  • :handle-error is a fn with a sig [exception request response]. Defaults to just rethrowing the Exception

wrap-json-response

(wrap-json-response handler & {:keys [predicate encoder type charset handle-error pretty], :or {predicate serializable?, pretty nil, type "application/json", charset default-charset-extractor, handle-error default-handle-error}})

Wrapper to serialize structures in :body to JSON with sane defaults. See wrap-format-response for more details.

wrap-restful-response

(wrap-restful-response handler & {:keys [predicate handle-error formats charset binary?], :or {handle-error default-handle-error, predicate serializable?, charset default-charset-extractor, formats [:json :yaml :edn :clojure :yaml-in-html :transit-json :transit-msgpack]}})

Wrapper that tries to do the right thing with the response :body and provide a solid basis for a RESTful API. It will serialize to JSON, YAML, Clojure, Transit or HTML-wrapped YAML depending on Accept header. See wrap-format-response for more details. Recognized formats are :json, :json-kw, :edn :yaml, :yaml-in-html, :transit-json, :transit-msgpack.

wrap-transit-json-response

(wrap-transit-json-response handler & {:keys [predicate encoder type handle-error options], :or {predicate serializable?, type "application/transit+json", handle-error default-handle-error}})

Wrapper to serialize structures in :body to transit over JSON with sane defaults. See wrap-format-response for more details.

wrap-transit-msgpack-response

(wrap-transit-msgpack-response handler & {:keys [predicate encoder type handle-error options], :or {predicate serializable?, type "application/transit+msgpack", handle-error default-handle-error}})

Wrapper to serialize structures in :body to transit over msgpack with sane defaults. See wrap-format-response for more details.

wrap-yaml-in-html-response

(wrap-yaml-in-html-response handler & {:keys [predicate encoder type charset handle-error], :or {predicate serializable?, encoder wrap-yaml-in-html, type "text/html", charset default-charset-extractor, handle-error default-handle-error}})

Wrapper to serialize structures in :body to YAML wrapped in HTML to check things out in the browser. See wrap-format-response for more details.

wrap-yaml-response

(wrap-yaml-response handler & {:keys [predicate encoder type charset handle-error], :or {predicate serializable?, encoder yaml/generate-string, type "application/x-yaml", charset default-charset-extractor, handle-error default-handle-error}})

Wrapper to serialize structures in :body to YAML with sane defaults. See wrap-format-response for more details.