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/readerthis function takes aString. - options
- Optional parameter. This is a map that will be passed to
cognitect.transit/readeras its third argument. Additionallydecode’soptionsmap can contain:encoding:- :encoding
- Transit reader’s encoding. Default is
:json. Passed toreaderas its second argument.
An example options map:
{:encoding :json :handlers {Foo (FooHandler.)} :default-handler (DefaultHandler.)}:encodingkey stripped fromoptionsbefore callingreader.
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/writeras 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 toreaderas its second argument.
An example options map:
{:buffer-size 4096 :encoding :json-verbose :handlers {Foo (FooHandler.)}}:buffer-size&:encodingkeys are stripped fromoptionsbefore callingwriter.
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
:readerand a:writerkeys which correspond to options to be passed to decode and encode respectively.[:reader :encoding]will always be overwritten using theContent-Typeheader 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.