Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way to set custom Content-Type for responses? #236

Closed
bard opened this issue Jun 30, 2016 · 4 comments
Closed

Correct way to set custom Content-Type for responses? #236

bard opened this issue Jun 30, 2016 · 4 comments
Labels

Comments

@bard
Copy link

bard commented Jun 30, 2016

I'd like to set a custom Content-Type for responses, to be able eventually to use something like application/vnd.foobar.v0+json.

I tried this:

(def app
  (api
   {:swagger
    {:ui "/api-docs"
     :spec "/swagger.json"
     :data {:info {:title "Sample API"}}}}

   (GET "/test" []
        :produces "text/plain"
        (ok "Hello, world!\n"))))

But the Content-Type header just disappears:

$ curl -D-  http://localhost:3000/test
HTTP/1.1 200 OK
Date: Thu, 30 Jun 2016 10:42:20 GMT
Content-Length: 14
Server: Jetty(7.6.13.v20130916)

Hello, world!

After seeing #235 I tried to set :format to {:formats []}, but it made no different. Not sure how #108 plays into this.

https://github.com/metosin/compojure-api/wiki/Swagger-integration says:

By default, the application wire-format serialization capabilities (:produces and :consumes) are injected in automatically by the api machinery.

But doesn't mention how to depart from the default.

I'm using compojure-api 1.1.3.

Is this supported?

@ikitommi
Copy link
Member

ikitommi commented Jul 9, 2016

Hi. Currently, you can do this by creating a custom formatter for the ring-middleware-format that compojure-api is using. See https://github.com/ngrunwald/ring-middleware-format for details how to write one. I guess it would work just like the :json-kw but with different contenty-type. You can install your formatter to compojure-api like this:

(api
  {:format {:formats [my-custom-json-formatter :edn ....]}}
   ...)

hope this helps. Should be much easier in future, about to rewrite r-m-f (async + faster).

ikitommi added a commit that referenced this issue Aug 1, 2016
@ikitommi
Copy link
Member

ikitommi commented Aug 1, 2016

with 1.1.6, this should be easier. Added test for this.

@ikitommi
Copy link
Member

ikitommi commented Aug 1, 2016

But the root-cause is here: ngrunwald/ring-middleware-format#53. Before that is fixed, you could add a middleware outside of the api, which reads the :content-type header and resets that.

@ikitommi
Copy link
Member

ikitommi commented Oct 6, 2016

closing this now. https://github.com/metosin/muuntaja will replace r-m-f soon.

@ikitommi ikitommi closed this as completed Oct 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants