Clojure: I am using http-kit to post a request to a server, but it is not working well for me

2.9k views Asked by At

NOTE: I resolved my issue. However, it took a number of incremental changes. If you happen upon this page, feel free to checkout my github below to see how I made this application work.


I am using http-kit to post a request to btc-china. I want to use their trading api. I am able to do this just fine with python, but for some reason I keep getting 401s with clojure and http-kit. I've posted a snippit of code below which may show that I am not using http-kit correctly. In addition to that, here is a github for my full code if you wish to look at that: https://github.com/gilmaso/btc-trading Here are the btc-china api docs: http://btcchina.org/api-trade-documentation-en

(def options {:timeout 2000 ; ms
          :query-params (sorted-map :tonce tonce
                                    :accesskey access-key
                                    :requestmethod request-method
                                    :id tonce
                                    :method method
                                    :params "")
          :headers {"Authorization" auth-string
                    "Json-Rpc-Tonce" tonce}})

(client/post (str "https://" base-url) options
      (fn [{:keys [status headers body error]}] ;; asynchronous handle response
        (if error
          (println "Failed, exception is " error)
          (println "Async HTTP GET: " status))))
2

There are 2 answers

3
noisesmith On BEST ANSWER

quoting from the example on the bttchina site:

# The order of params is critical for calculating a correct hash

clojure hash maps are unordered, and you cannot use a clojure hash map literal to provide the input if order is significant

0
VitoshKa On

I had very similar problem with bitstamp api. The solution was to replace :query-params with :form-params. Then the parameters are sent in the body. I noticed that in your api you are manually sending then in the body. It looks like using :form-params might help in your case as well.