Liking cljdoc? Tell your friends :D

Slash handler

The router works with precise matches. If a route is defined without a trailing slash, for example, it won't match a request with a slash.

(require '[reitit.ring :as ring])

(def app
  (ring/ring-handler
    (ring/router
      ["/ping" (constantly {:status 200, :body ""})])))

(app {:uri "/ping/"})
; nil

Sometimes it is desirable that paths with and without a trailing slash are recognized as the same.

Setting the redirect-trailing-slash-handler as a second argument to ring-handler:

(def app
  (ring/ring-handler
    (ring/router
      [["/ping" (constantly {:status 200, :body ""})]
       ["/pong/" (constantly {:status 200, :body ""})]])
    (ring/redirect-trailing-slash-handler)))

(app {:uri "/ping/"})
; {:status 308, :headers {"Location" "/ping"}, :body ""}
(app {:uri "/pong"})
; {:status 308, :headers {"Location" "/pong/"}, :body ""}

redirect-trailing-slash-handler accepts an optional :method parameter that allows configuring how (whether) to handle missing/extra slashes. The default is to handle both.

(def app
  (ring/ring-handler
    (ring/router
      [["/ping" (constantly {:status 200, :body ""})]
       ["/pong/" (constantly {:status 200, :body ""})]])
    ; only handle extra trailing slash
    (ring/redirect-trailing-slash-handler {:method :strip})))

(app {:uri "/ping/"})
; {:status 308, :headers {"Location" "/ping"}, :body ""}
(app {:uri "/pong"})
; nil
(def app
  (ring/ring-handler
    (ring/router
      [["/ping" (constantly {:status 200, :body ""})]
       ["/pong/" (constantly {:status 200, :body ""})]])
    ; only handle missing trailing slash
    (ring/redirect-trailing-slash-handler {:method :add})))

(app {:uri "/ping/"})
; nil
(app {:uri "/pong"})
; {:status 308, :headers {"Location" "/pong/"}, :body ""}

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close