Fork me on GitHub
#ring
<
2023-12-19
>
Sam14:12:39

How do I return a CSV response? I want the user to be able to curl and endpoint and they should up with a csv file.

dharrigan15:12:57

create an io/writer, write into that, then feed it into the http://ring.util.io like this:

❤️ 1
dharrigan15:12:48

(-> (your-writer-function)
          (rio/piped-input-stream)
          (response/response)
          (response/content-type "text/csv; charset=UTF-8")
          (response/header "Content-Disposition"  "attachment; filename=foobar.csv"))))

❤️ 1
Sam15:12:49

Thank you!

dharrigan15:12:00

you're most welcome.

❤️ 1
simple_smile 1
Sam12:12:46

I'm testing this out. How do I create the io/writer? I have this now:

(defn write-csv
  "Takes a file (path, name and extension) and 
   csv-data (vector of vectors with all values) and
   writes csv file."
  [file csv-data]
  (with-open [writer (io/writer file)]
    (csv/write-csv writer csv-data)))
So I have tho io/writer, but it's writing to a file. And it's being written to by csv/write-csv. I'm uncertain about what goes where.