biff icon indicating copy to clipboard operation
biff copied to clipboard

Have starter app include example of returning html with full response map

Open jacobobryant opened this issue 1 year ago • 2 comments

See this comment: https://github.com/jacobobryant/biffweb.com/pull/4#issuecomment-2307718376

jacobobryant avatar Aug 28 '24 17:08 jacobobryant

Something like this? Happy to open up a PR.

diff --git a/starter/src/com/example/app.clj b/starter/src/com/example/app.clj
index 265dab1..4b37244 100644
--- a/starter/src/com/example/app.clj
+++ b/starter/src/com/example/app.clj
@@ -142,6 +142,21 @@
    :headers {"content-type" "application/json"}
    :body params})
 
+(defn custom-response
+  "Any biff handler that returns a vector is
+   automatically converted into html (see source
+   for com.biffweb.impl.middleware/wrap-render-rum).
+   If you need to escape this behavior, return
+   a ring response map instead."
+  [_ctx]
+  {:status 200
+   :headers {"content-type" "text/html"}
+   :cookies {"foo" {:value "bar"
+                    :max-age (* 365 86400)}}
+   :body (rum/render-static-markup
+           [:code "Open your browser console to view
+                   the custom headers set by biff."])})
+
 (def module
   {:static {"/about/" about-page}
    :routes ["/app" {:middleware [mid/wrap-signed-in]}
@@ -149,5 +164,6 @@
             ["/set-foo" {:post set-foo}]
             ["/set-bar" {:post set-bar}]
             ["/chat" {:get ws-handler}]]
-   :api-routes [["/api/echo" {:post echo}]]
+   :api-routes [["/api/echo" {:post echo}]
+                ["/api/custom-response" {:get custom-response}]]
    :on-tx notify-clients})

harismh avatar Oct 11 '24 20:10 harismh

Yep, like that. No need for a PR--I still need to decide whether this should be an additional route, or if I should just add some comments to the existing echo handler which already returns a map...

jacobobryant avatar Oct 12 '24 22:10 jacobobryant