metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.12k stars 149 forks source link

How do you set content type while still encoding? #390

Closed kanwei closed 6 years ago

kanwei commented 6 years ago

Library Version(s)

alpha-23: I see that BREAKING: Don't encode response :body if Content-Type header is set.

Problem

We have an endpoint that returns a CSV as a stream:

{:headers {"Content-Type" "text/csv"}
         :body    (ring-io/piped-input-stream
                    (fn [ostream]
                      (let [^java.io.Writer w (clojure.java.io/make-writer ostream {})]
                        (data-csv/write-csv w (util/map->csv-lazily records))
                        (.flush w))))}

This now returns nothing. How are we now supposed to add the content-type but still getting through the encoding stack?

ikitommi commented 6 years ago

How did the data get encoded earlier? Do you have a csv-format for Muuntaja?

kanwei commented 6 years ago

I just use this in the middleware:

             (muuntaja/wrap-params)
             (muuntaja/wrap-format)

It seems to coerce the piped-input-stream to an actual output. In alpha-23 no data is returned at all.

ikitommi commented 6 years ago

That muuntaja change shoudn't affect it. With default configuration, Muuntaja doesn't touch the body if it's a stream, the Streaming of InputStream is handled by the Ring Adapter.

So I think that the problem is somewhere else. Do you have :return or :responses defined for that route? The same code works with the previous version?

kanwei commented 6 years ago

You're right, I can no longer reproduce this.