4

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))))
gilmaso
  • 219
  • 4
  • 8
  • Have you tried comparing the actual HTTP exchanges in either case? Maybe seeing the difference would give you a hint. – 9000 Dec 09 '13 at 06:09
  • 9000, I am actually not sure how to do that in clojure or python :/ – gilmaso Dec 09 '13 at 07:10
  • 1
    a program like [wireshark](http://www.wireshark.org/) will let you record http traffic, and analyze it at your convenience – noisesmith Dec 10 '13 at 00:08

2 Answers2

4

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

noisesmith
  • 20,076
  • 2
  • 41
  • 49
  • I figured that the order would only matter for the hash itself but not for the query parameters. That being said, it looks like both clj-http and http-kit only allow for hashmaps for their query parameters. – gilmaso Dec 09 '13 at 07:11
  • @user3081629 you can use sorted-map/sorted-map-by (or just sort map) for hash calculation – edbond Dec 09 '13 at 09:12
  • edbond, I went back and changed the map to a sorted-map. I should have thought of that myself, but I'm really new to clojure. That being said, the 401s still persist. – gilmaso Dec 09 '13 at 17:25
1

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.

VitoshKa
  • 8,387
  • 3
  • 35
  • 59