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

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

Open
jacobobryant opened this issue Aug 28, 2024 · 2 comments

Comments

@jacobobryant
Copy link
Owner

See this comment: jacobobryant/biffweb.com#4 (comment)

@harismh
Copy link
Contributor

harismh commented Oct 11, 2024

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})

@jacobobryant
Copy link
Owner Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants