Search code examples
http-redirectclojurering

ring redirect after login


(ns ...
  (:require  [ring.util.response :refer [ response redirect]))

My original code be-all-like

(-> (response "You are now logged in! communist party time!")
    (assoc :session new-session)
    (assoc :headers {"Content-Type" "text/html"}))

Which worked well, but the user still has to navigate elsewhere manually.

Trying to use http://ring-clojure.github.io/ring/ring.util.response.html#var-redirect

(-> (redirect requri)
    (assoc :session new-session)
    (assoc :headers {"Content-Type" "text/html"}))

doesn't do anything (aside from returning a blank page).

How can I achieve a redirect to a known uri using ring?


Solution

  • response is return a body. you code is (response requri),but the param of the funtion reponse is html body,not a uri,you can use the this function

    like this

    (ns foo
       (:require [ring.util.response :as response]))
    (def requi "/")
    (-> (response/redirect requri)
        (assoc :session new-session)
        (assoc :headers {"Content-Type" "text/html"}))
    

    ps: if you are writing a web site.the lib-noir is a good way to control the session and other.