This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-04
Channels
- # beginners (8)
- # boot (20)
- # cider (8)
- # cljs-dev (263)
- # cljsjs (8)
- # cljsrn (20)
- # clojure (151)
- # clojure-argentina (1)
- # clojure-belgium (7)
- # clojure-dev (18)
- # clojure-italy (25)
- # clojure-spec (34)
- # clojure-uk (15)
- # clojurescript (89)
- # component (45)
- # core-async (27)
- # cursive (16)
- # datomic (53)
- # emacs (40)
- # figwheel (3)
- # hoplon (62)
- # jobs (1)
- # jobs-discuss (7)
- # luminus (8)
- # lumo (60)
- # off-topic (3)
- # parinfer (1)
- # precept (1)
- # protorepl (15)
- # re-frame (37)
- # reagent (7)
- # ring (3)
- # ring-swagger (73)
- # slack-help (1)
- # specter (19)
- # sql (4)
- # test-check (10)
- # uncomplicate (2)
- # unrepl (14)
- # untangled (52)
- # vim (5)
- # yada (42)
@lambder it's just a ring-handler, so if nothing is consuming the request body (e.g. no mappings in muuntaja for text/plain), you can slurp the :body
and get the string. Adding :consumes ["text/plain"]
also pushes that to swagger.
:parameters {:body-params s/Str
:query-params {:version s/Str
:rule_name s/Str
:force_overwrite s/Str}}
That works if something handles the text/plain before the actual resource handlee. You can configure muuntaja to do that, either for api, subroutes or just for that resource. Here is the guide howto add new decoders: https://github.com/metosin/muuntaja/wiki/Creating-new-formats.
There can be any number of muuntaja mws on the mw-chain, the first matching one does things, rest are ni-op.
in https://github.com/metosin/muuntaja/wiki/Creating-new-formats there is:
(def m
(muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "application/upper-json"]
streaming-upper-case-json-format)))
(->> {:kikka 42}
(muuntaja/encode m "application/json")
(muuntaja/decode m "application/upper-json"))
:formats [(muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "application/upper-json"]
streaming-upper-case-json-format))]
unwrap the vector, here is sample: https://github.com/metosin/compojure-api/blob/master/test/compojure/api/integration_test.clj#L1456-L1467
@ikitommi my resource is:
(resource
{
:post {
:parameters {:body-params s/Str
:query-params {:version s/Str
:rule_name s/Str
:force_overwrite s/Str}}
:formats (muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "text/plain"]
{:decoder identity}))
:consumes ["text/plain"]
:responses {http-status/created {:schema rest-schema/GenericResult}}
...
POST /api/store_rule?rule_name=foo.logic&force_overwrite=true&version=1.1 HTTP/1.1
Host: localhost:7777
Content-Type: text/plain
Accept: application/json
TEXT
i got
{
"schema": "java.lang.String",
"errors": "(not (instance? java.lang.String nil))",
"type": "compojure.api.exception/request-validation",
"coercion": "schema",
"value": null,
"in": [
"request",
"body-params"
]
}
my bad, the :formats
is currently only mapped to api. To use that in resource, you should use it via :middleware
. something like: :middleware [[muuntaja.middleware/wrap-format options]]
. And the :middleware
is in the 2.0.0-alpha.
:middleware [[muuntaja.middleware/wrap-format (muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "text/plain"]
{:decoder identity}))]]
(api
{:swagger
{:ui "/"
:spec "/swagger.json"
:data {
}
:coercion :schema
:formats (muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "text/plain"]
{:decoder identity}))
}
(context "/api" []
(context "/store_rule" []
(resource
{
:post {
same thing for
:formats (muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "text/plain"]
{:decoder (constantly identity)}))
same problem for :
:formats (muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "text/plain"]
{:decoder [(constantly identity)]}))
https://github.com/metosin/muuntaja/blob/master/test/muuntaja/core_test.clj#L194-L204
it can be either a function or a duct-stylr vector. But ring passes in an InputStream, so (constantly slurp)
to read it to string.
:formats (muuntaja/create
(assoc-in
muuntaja/default-options
[:formats "text/plain"]
json-format/json-format))
I'm a week away from computer, can't test :( maybe @juhoteperi or @miikka could try to help
@ikitommi so this
:formats {:default-format "text/plain"
:formats {"text/plain" streaming-upper-case-json-format
"application/json" streaming-upper-case-json-format
"application/vnd.api+json" streaming-upper-case-json-format}}
had the effectoh, that's a bug then. Here's a guide how to use muuntaja with ring, should work the same with c-api
no, once created it a Record instance with lot's of internals for speed. But the create
takes both options and records and returns an record.
or it rather should be:
(context "/store_rule" []
(resource
{:middleware [...]
:post {
both are ok. As the docs are far from good, you can look at the tests. The resource-test should cover most/all legal confifurations.
this worked:
(def text-plain-format
{:decoder [(fn make-json-decoder [options]
(fn [x ^String charset]
(if (string? x)
x
(slurp (InputStreamReader. ^InputStream x charset)))))]
:encoder [(fn make-json-encoder [options]
(fn [data ^String charset]
(ByteArrayInputStream. (.getBytes data charset))))]})
(resource {
:post {
:middleware [[muuntaja.middleware/wrap-format
(muuntaja/create
(assoc-in muuntaja/default-options
[:formats "text/plain"] text-plain-format))]]
...