passing optional :query-param in clojure request

202 Views Asked by At

I have started learning clojure and have one use case where I would want to call a downstream service with query-params. Now these query-params can vary and can be absent. I am stuck at how to achieve it using clojure. Any help from experience folks would be great!

(defn- call-stud-service* [name id college_id course city]
  (let [base-url-prefix       (get-in config [:url :student-service])
        student-service-url   (str (url base-url-prefix base-path))]
    {:connection-manager connection-manager
              :throw-exceptions   false
              :headers            {:Content-Type        "application/json"
                                   :Accept              "application/json"}
              :query-params       {"name"       name
                                   "id"         id
                                   "college_id" college_id
                                   "course"     course}}))

What my use case is : if I have additional parameter city I can pass it in the request as query-param else I won't.

I gave a thought maybe using something assoc to append query-params but not sure if would work.

1

There are 1 best solutions below

0
Eugene Pakhomov On

Use cond->:

(cond-> query-params
  city (assoc "city" city))