CLJS: generate "GET" http request string

My code has a (js/Audio. "path-string") portion, where that path string needs to be a GET string with arguments. It works if I write that string by hand, and those strings are auto-generated elsewhere in the guts of cljs-ajax, but I couldn’t find in that codebase any function I could use. I’m loathe to roll my own for what must be a highly common use case. What can I use in cljs to generate strings like 'http://example.com?arg1=3&arg2=5`?

You probably mean query parameters.

From the README:

:params - the parameters that will be sent with the request, format dependent: :transit and :edn can send anything, :json, :text and :raw need to be given a map. GET will add params onto the query string, POST will put the params in the body

e.g.: :params {:arg1 3, :arg2 5}

1 Like

You might look into using some some closure functions. (goog.uri.utils)
See https://google.github.io/closure-library/api/goog.uri.utils.html#appendParamsFromMap

(require
 '[goog.string :as string]
 '[goog.uri.utils :as uri])

(uri/appendParamsFromMap "https://example.com"
                         #js {:a 1
                              :b (string/urlEncode "example query param")})

https://example.com?a=1&b=example%2520query%2520param

4 Likes

Another option is the WHATWG URL class

(doto (js/URL. "http://example.com/query")
  (goog.object/set
   "search"
   (doto (js/URLSearchParams.) (.append "k1" "v1") (.append "k2" "v2"))))

This should work in modern browsers and can be made to work in node as well.

1 Like

goog.uri.utils is super nice, I didn’t know about that namespace. Another hidden GCL gem.

1 Like

Yep; that’s the syntax for the actual AJAX requests. Now I don’t want a request – just a string

This looks great! I’ve got to sit down and just study closure lib sometime, somehow.

1 Like

Sorry, misunderstood the question.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.