How to send http requests the curl way? or: help with http-clj

I just want to convert CURL examples to clojure code.
I’m trying to send

curl -X POST --header "Content-Type: application/x-www-form-urlencoded" -d "client_id=MYCLIENT_ID&scope=api&client_secret=SECRET&grant_type=client_credentials" API_URL

with clj-http and I’m really struggling

So far, I’ve tried things like this:

(require '[clj-http.client :as client])

(client/post  (:authorize-uri client-params)
                {:save-request? true
                 :debug true :debug-body false
                 :headers
                 {:content-type "application/x-www-form-urlencoded"
                  "-d"
                  (str
                   "client_id="
                   (:client-id client-params)
                   "&scope="
                   (:scope client-params)
                   "&client_secret="
                   (:client-secret client-params)
                   "&grant_type="
                   (:grant-type client-params))}})

But honestly, I just want curl mechanics, make the damn string, and don’t care about obtuse nested maps… Every example out there is curl, So I want to speak curl as well in my programs. building strings from data is easy enough, I don’t need a library to do that for me, It only becomes more cognitive overhead with no added benefit. (sorry for the rant)

I keep getting failures from the server when sending it from clojure, and from the debug info it seems that there is additional data, like “accept-encoding” “gzip, deflate” injected into the header by some middleware in the library.

Any help is really appreciated.

Perhaps you can use GitHub - babashka/babashka.curl: A tiny curl wrapper via idiomatic Clojure, inspired by clj-http, Ring and friends.. With the :debug setting it will return the command string it used to call curl. You can also pass raw arguments with :raw-args.

You can use :query-params. Also note that save-request, debug, debug-body are not needed

(require '[clj-http.client :as http])

(http/post "http://xys.zom"
  {:save-request? true,
   :debug true,
   :headers {:content-type "application/x-www-form-urlencoded"}
   :query-params {:client_id "234"
                  :scope "realm"
                  :client_secret "asd"}})
3 Likes