Search code examples
clojurering

Get URL parameters from ring


Starting with string "http://www.youtube.com/watch?v=P76Vbsk_3J0", I'd like to get {:v P76Vbsk_3J0}.

I am nearly there with:

(#'ring.middleware.params/parse-params "http://www.youtube.com/watch?v=P76Vbsk_3J0" "UTF-8")
=> {"http://www.youtube.com/watch?v" "P76Vbsk_3J0"}

(clojure.walk/keywordize-keys {"http://www.youtube.com/watch?v" "P76Vbsk_3J0"})
=> {:http://www.youtube.com/watch?v "P76Vbsk_3J0"}

Is there something I can grab from ring to get me the rest of the way there?


Solution

  • ring.middleware.params/parse-params expects only to receive a query parameter string so you need to remove everything before and including the ?, something like this will work:

    (#'ring.middleware.params/parse-params
      (second (.split "http://www.youtube.com/watch?v=P76Vbsk_3J0" "\\?"))
      "UTF-8")
    ;=> {"v" "P76Vbsk_3J0"}
    

    Note that you are using a function that is not a part of the public API.