How to add headers to a clojure.java.io/resource response

197 views
Skip to first unread message

Jonathon McKitrick

unread,
May 26, 2015, 8:22:10 AM5/26/15
to clo...@googlegroups.com
I have a GET route returning the result of this:

(io/resource "public/html/confirm.html")

but I need to add Cache-Control headers.  Since the `resource` function returns a java.net.URL object, how can I add headers?  The normal Ring way with ring.util.response/header only operates on a Ring response.

James Reeves

unread,
May 26, 2015, 8:43:21 AM5/26/15
to clo...@googlegroups.com
Compojure uses the compojure.response/render protocol method to turn values like URLs into Ring responses. So you could write:

    (-> (io/resource "public/html/confirm.html")
        (compojure.response/render)
        (response/header "X-Foo" "Bar"))

Or you could use some middleware, if the header is standard across your application. Or since you know that you're delivering a HTML file, you could also write:

    (-> (response/resource-response "public/html/confirm.html")
        (response/content-type "text/html; charset=utf-8")
        (response/header "X-Foo" "Bar"))

That should result in the same thing, as the only thing Compojure does that Ring doesn't is try to make an educated guess about the content type.

- James

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathon McKitrick

unread,
May 27, 2015, 8:01:54 AM5/27/15
to clo...@googlegroups.com, ja...@booleanknot.com
Hmm.  I tried this:

(-> (io/resource "public/html/confirm.html")
             (cr/render)
             (resp/header "Cache-Control" "no-cache, no-store"))

and got this:

Exception in thread "main" java.lang.IllegalArgumentException: No single method: render of interface: compojure.response.Renderable found for function: render of protocol: Renderable, compiling:(pts/server.clj:483:14)

James Reeves

unread,
May 27, 2015, 8:12:10 AM5/27/15
to clo...@googlegroups.com
Oh, my apologies. You also need to pass the request map to the render method.

    (-> (io/resource "public/html/confirm.html")
        (cr/render request)

        (resp/header "Cache-Control" "no-cache, no-store"))


You could also write it as:

    (-> (resp/resource-response "public/html/confirm.html)
        (resp/content-type "text/html; charset=utf-8")
        (resp/header "Cache-Control" "no-cache, no-store"))

Or write some middleware:

    (defn wrap-cache-control [handler cache-control]
      (fn [request]
        (some-> (handler request)
                (resp/header "Cache-Control" cache-control))))

- James

Jonathon McKitrick

unread,
May 27, 2015, 10:05:22 AM5/27/15
to clo...@googlegroups.com, ja...@booleanknot.com
I'll give that a shot, thanks!
Reply all
Reply to author
Forward
0 new messages