ring.middleware.transit

Ring middleware for Transit requests & responses.

For basic usage you just need to add wrap-transit middleware.

Usage Example:

(require '[ring.middleware.transit :refer [wrap-transit]])


(defn handler [request]
  (let [username (get-in request [:params :username])]
    {:hello username}))


(defn app
  (-> handler
      (wrap-transit)))

encode & decode are provided for reusing of options to wrap-transit when encoding/decoding Transit outside of HTTP requests/responses. When using WebSockets or communicating with other services.

If you want to write a custom middleware based on this code take a look at -decode-request & -encode-response functions. They are not documented.

decode

(decode s)(decode s options)

Decode string Transit data.

Parameters:

s
Transit data. Note that unlike cognitect.transit/reader this function takes a String.
options
Optional parameter. This is a map that will be passed to cognitect.transit/reader as its third argument. Additionally decode’s options map can contain :encoding:
:encoding
Transit reader’s encoding. Default is :json. Passed to reader as its second argument.

An example options map:

{:encoding :json
 :handlers {Foo (FooHandler.)}
 :default-handler (DefaultHandler.)}

:encoding key stripped from options before calling reader.

encode

(encode v)(encode v options)

Encode some value into string Transit data.

Parameters:

v
Value to be encoded.
options
Optional parameter. This is a map that will be passed to cognitect.transit/writer as its third argument. Additionally there are two more keys accepted:
:buffer-size
Size of the buffer of the output stream, in bytes. Default is 1024.
:encoding
Transit writer’s encoding. Default is :json. Passed to reader as its second argument.

An example options map:

{:buffer-size 4096
 :encoding :json-verbose
 :handlers {Foo (FooHandler.)}}

:buffer-size & :encoding keys are stripped from options before calling writer.

wrap-transit

(wrap-transit handler)(wrap-transit handler options)

Decodes Transit requests and encodes Transit responses.

Parameters:

handler
Ring handler to wrap.
options
Optional parameter. A map of options that can contain a :reader and a :writer keys which correspond to options to be passed to decode and encode respectively.

[:reader :encoding] will always be overwritten using the Content-Type header of the request.

Transit Requests

Decoded Transit messages can be accessed through request’s :transit-params key. If the decoded object is a map, it will be also be merged with request’s :params.

For Transit requests, :body is read into a string and is available to downstream.

Transit Responses

If there is no Content-Type header, anything but the types ring accepts as valid response bodies are encoded. If Content-Type is present it overrides the type of :body.

Content Type Header Response Type Encoded?
Not present. String, InputStream, File or ISeq No.
Not present. Anything else. Yes.
application/transit Anything. Yes.
Other content type Anything. No.

Aleph Support

If you have Aleph in your classpath deferred responses will be handled properly using manifold.deferred/chain.