Clojure building of URL from constituent parts

2019-03-18 10:48发布

In Python I would do the following:

>>> q = urllib.urlencode({"q": "clojure url"})
>>> q
'q=clojure+url'

>>> url = "http://stackoverflow.com/search?" + q
>>> url
'http://stackoverflow.com/search?q=clojure+url'

How do I do all the encoding that's done for me above in Clojure? In other words, how do I do something akin to the following:

=> (build-url "http://stackoverflow.com/search" {"q" "clojure url"})
"http://stackoverflow.com/search?q=clojure+url"

标签: url clojure
5条回答
再贱就再见
2楼-- · 2019-03-18 11:22

clj-apache-http is pretty useful. With it you can do the following:

user=> (require ['com.twinql.clojure.http :as 'http])
nil
user=> (def q (http/encode-query {"q" "clojure url"}))
#'user/q
user=> (def url (str "http://stackoverflow.com/search?" q))
#'user/url
user=> url
"http://stackoverflow.com/search?q=clojure+url"
查看更多
在下西门庆
3楼-- · 2019-03-18 11:31

There is a url-encode function in Ring's ring.util.codec namespace:

(ring.util.codec/url-encode "clojure url")
; => "clojure+url"

I'm not sure if there's a prepackaged function to turn a map into a query string, but perhaps this could do the job:

(use '[ring.util.codec :only [url-encode]])

(defn make-query-string [m]
  (->> (for [[k v] m]
         (str (url-encode k) "=" (url-encode v)))
       (interpose "&")
       (apply str)))

An example:

user> (make-query-string {"q" "clojure url" "foo" "bar"})
"q=clojure+url&foo=bar"

All that remains is concatenating the result onto the end of a URL:

(defn build-url [url-base query-map]
  (str url-base "?" (make-query-string query-map)))

Seems to work:

user> (build-url "http://stackoverflow.com/search" {"q" "clojure url"})
"http://stackoverflow.com/search?q=clojure+url"

Update:

Perhaps a modified version might make for a more Clojure-friendly experience. Also handles encoding via a Ring-style optional argument with utf-8 as default.

(defn make-query-string [m & [encoding]]
  (let [s #(if (instance? clojure.lang.Named %) (name %) %)
        enc (or encoding "UTF-8")]
    (->> (for [[k v] m]
           (str (url-encode (s k) enc)
                "="
                (url-encode (str v) enc)))
         (interpose "&")
         (apply str))))

(defn build-url [url-base query-map & [encoding]]
  (str url-base "?" (make-query-string query-map encoding)))

So now we can do

user> (build-url "http://example.com/q" {:foo 1})
"http://example.com/q?foo=1"
查看更多
走好不送
4楼-- · 2019-03-18 11:36

A simple solution using str(ings):

(def q (str "clojure+url"))
(def url (str "http://stackoverflow.com/search?" q))
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-18 11:38

Here's one way:

user=> (import [java.net URLEncoder])                                                      
java.net.URLEncoder
user=> (str "http://stackoverflow.com/search?q=" (URLEncoder/encode "clojure url" "UTF-8"))
"http://stackoverflow.com/search?q=clojure+url"

I know this is not exactly the same as your Python snippet though. Please see the following post from the Clojure mailing list for a more complete answer:

http://www.mail-archive.com/clojure@googlegroups.com/msg29338.html

The code from there will allow you to do this:

user=> (encode-params {"q" "clojure url"})
"q=clojure+url"
查看更多
家丑人穷心不美
6楼-- · 2019-03-18 11:47

This is the exact REPL equivalent of your python session, using clj-http.

user=> (require ['clj-http.client :as 'client])
nil
user=> (str "http://stackoverflow.com/search?"
user=*      (client/generate-query-string {"q" "clojure url"}))
"http://stackoverflow.com/search?q=clojure+url"

but clj-http makes it even easier:

user=> (client/get "http://stackoverflow.com/search?"
user=*             {:query-params {"q" "clojure url"}})
... <a lot of output, omitted to protect the innocent>...

assuming that you want to perform a GET request, that is.

查看更多
登录 后发表回答