How to get request parameters in clojurescript?

I use a secretary and a reagent. This is my code:

(def view (atom nil)) (defn layout [view] [:div @view]) (reagent/render-component [layout view] (.getElementById js/document "message")) (secretary/set-config! :prefix "") (secretary/defroute home-path "/" [query-params] (timbre/info "Path : /, query params : " query-params) (let [warning (:warning query-params) success (:success query-params) login-failed (:login_failed query-params)] (when warning (timbre/info "Warning found : " warning) (reset! view [:h4 [:span.label.label-warning warning]])) (when success (timbre/info "Success found : " success) (reset! view [:h4 [:span.label.label-info success]])) (when login-failed (timbre/info "Login failed") (reset! view [:h4 [:span.label.label-warning "Login Failed."]])))) (let [h (History.)] (goog.events/listen h EventType.NAVIGATE #(secretary/dispatch! (.-token %))) (doto h (.setEnabled true))) 

Excluding the prefix value (I tried "," # ", and also did not set the prefix: this code only works with routes such as:

 http://localhost:8080/login#/?success=SuccessMessage 

But it does not work with such routes as:

 http://localhost:8080/login?success=SuccessMessage 

What I'm actually trying to achieve is to analyze the login failure from friend , which in case of failure redirects me to

 http://localhost:8080/login?&login_failed=Y&username=someUser 

and display a login error message to the user. I do not need to use a secretary for this , everything that works to analyze the request parameters would be ok for me.

The hard way would be to parse the query string with which I can get:

 (-> js/window .-location .-search) 

I believe this is already well done in some library.

+9
clojurescript reagent secretary
source share
2 answers

I found him. Using https://github.com/cemerick/url (works for both clojure and clojurescript), you can do:

 (require '[cemerick.url :as url]) (:query (url/url (-> js/window .-location .-href))) 
+15
source share

From the docs :

If the URI contains a query string, it will be automatically extracted to: query-params for string route matchers and to the last element for regular expression matchers.

 (defroute "/users/:id" [id query-params] (js/console.log (str "User: " id)) (js/console.log (pr-str query-params))) (defroute #"/users/(\d+)" [id {:keys [query-params]}] (js/console.log (str "User: " id)) (js/console.log (pr-str query-params))) ;; In both instances... (secretary/dispatch! "/users/10?action=delete") ;; ... will log ;; User: 10 ;; "{:action \"delete\"}" 
0
source share

All Articles