powered by text files


Following Url Redirects with Clojure

In attempting to normalize urls for a project, I needed to unravel the redirects.

Urls that are provided could be via a URL shortening service, an RSS feed or from another source. To compare them, I need to determine the final destination of the URL, not what is initially supplied.

To solve this in Clojure, I used the class to open a connection. By calling .getResponse, the connection will follow through the redirects. If this is successful, I return the final URL, otherwise I give back the initial URL.

(def test-url "")
(defn resolve-redirect [initial-url]
  "Follows any redirects to the supplied url and returns the final destination"
(let [url ( initial-url)
      conn (.openConnection url)]
  (if (= HttpURLConnection/HTTP_OK (.getResponseCode conn))
    (.. conn getURL toString)

I’m still to test this out on a wide variety of production uses, so your milage may vary.

Note: You’ll need to include the HttpURLConnection with (:import ( HttpURLConnection)) in your namespace declaration.