Absolute Route URLs with Ring and Compojure

I need to redirect users to an absolute url after oAuth authentication.

How do I create an absolute url for a Compojure route? It looks like non-AJAX HTTP requests omit the header Origin

. Is there a Ring or Compojure helper for generating absolute URLs, or should I do it manually with schema and headers Host


Finally, and probably deserves a separate question, are there helper functions in Compojure to generate handler-based route urls, ala Html.ActionLink(...)

in MVC land?


source to share

1 answer

The ring-headers project has middleware that translates relative to absolute URLs:

(ns ring.middleware.absolute-redirects
  "Middleware for correcting relative redirects so they adhere to the HTTP RFC."
  (:require [ring.util.request :as req])
  (:import  [java.net URL MalformedURLException]))

(defn- url? [^String s]
  (try (URL. s) true
       (catch MalformedURLException _ false)))

(defn absolute-url [location request]
  (if (url? location)
    (let [url (URL. (req/request-url request))]
      (str (URL. url location)))))




All Articles