Fork me on GitHub
#reitit
<
2019-10-31
>
dharrigan12:10:10

Is there a way to apply a content-type for all returned data, i.e., if I know that I'll always be returning JSON, to set that as applicable for every response? At the moment, I'm setting it within the response map that I return, i.e.,

dharrigan17:10:47

However, if I don't require (or need) swagger for my use-case, it's a bit overkill to depend upon it

ikitommi17:10:44

swagger-data is inferred from routes, parameter and response definitions. Just don’t mount the swagger-spec & swagger-ui endpoints and it’s gone 🙂

dharrigan17:10:49

ah, wondeful

dharrigan12:10:31

{:status 200
     :headers JSON
     :body (utils/map->string results)}

dharrigan12:10:59

where JSON is def'ed as (def ^:const JSON {"Content-Type": "application/json"})

valerauko13:10:52

you can put :produces #{"application/json"} on the top of your routes

dharrigan13:10:33

scweet, trying...

dharrigan13:10:37

That didn't quite work as expected

dharrigan13:10:39

["/starships/:id" {:get get-starship-by-id
                       :produces #{"application/json"}
                       :parameters {:path {:id s/Int}}}]]

dharrigan13:10:42

wrong place?

ikitommi17:10:20

if you have Muuntaja middleware mounted, any response with a collection body and without Content-Type header is encoded into json/edn/transit, depending on what the client requested. It defaults to json (e.g. client is ok with anything)

ikitommi17:10:34

e.g. {:status 200, :body {:this-is "Sparta!"}}

ikitommi17:10:11

I don't think :produces does anything currently @vale?

valerauko02:11:08

wow. i totally thought muuntaja used that

eoliphant20:10:46

Hi there a way to abort navigation for the front end stuff? on-navigate gets called after it’s happened?

dharrigan20:10:12

@ikitommi thanks for the above tip, I can confirm that if the response body is a collection and if the muuntaja middleware is being used and if no content-type header is being set and if the client requests accept: application/json then yes, the response does come back as content-type: application/json

dharrigan20:10:23

but...you can probably see where I'm going with this...

dharrigan20:10:42

if the response is not a collection, but the rest (of the above) holds true

dharrigan20:10:10

then the response has no content-type set and is served up "as-is"

dharrigan20:10:52

I wouldd have thought, that if the accept had been set to application/json, the result would try to return (by default) content-type application/json

dharrigan20:10:47

i.e., here's the request/response

dharrigan20:10:29

my sincere apologies, it still works - I forgot to re-eval my changes!

dharrigan20:10:37

all is well 🙂

partywombat 8