Fork me on GitHub
#beginners
<
2015-08-01
>
coyotespike20:08:18

I'm trying to translate a curl call into Clojure, using clj-http

coyotespike20:08:38

Using the right subdomain, email, and token, this works: ''' curl -X POST 'https://my-sub-domain.slack.com/api/users.admin.invite' \ --data '[email protected]&token=supersecretoken&set_active=true' \ --compressed `

coyotespike20:08:01

It invites a user, , using my token, supersecretoken

coyotespike20:08:06

Which is kind of cool simple_smile

coyotespike20:08:44

Sadly, though, this does not work:

coyotespike20:08:47

(require '[clj-http.client :as http])
 (require '[clojure.data.json :as json])
(http/post "" 
           {:body 
            (json/json-str 
             {"json" 
              {:email ""
               :token "supersecretoken"
               :set_active true}})
            :body-encoding "UTF-8"
            :content-type :json
            :accept :json})

coyotespike20:08:46

I get a response which tells me I didn't include a token in my request.

niwinz20:08:24

With curl you are sending the data using form encoding and in clojure code you are enconding it using json. In a very quick view, this is a thing that can stay wrong

coyotespike20:08:43

Hmm...fair point, though it seems it should be possible to translate the HTTP call to Clojure...

coyotespike20:08:05

Ah, I found it

coyotespike20:08:24

(http/post "" 
           {:multipart
              [{:name "email" :content ""}
               {:name "token" :content "supersecretoken"}
               {:name "set_active" :content "true"}]})

niwinz20:08:37

I think you should use (client/post "" {:form-params {:foo "bar"}})

coyotespike20:08:19

@niwinz: I bet you're right that there is a way to use form-params as well

niwinz20:08:23

Multipart is only required if you want send files.

coyotespike20:08:41

You are right, this works also:

coyotespike20:08:46

(http/post "" 
           {:form-params
              {:email ""
              :token "supersecretoken"
               :set_active "true"}})

coyotespike20:08:42

Good to learn more about how to use this - thanks for your help! simple_smile

niwinz20:08:56

You are welcome!