【问题标题】:Clojure: I am using http-kit to post a request to a server, but it is not working well for meClojure:我正在使用 http-kit 向服务器发布请求,但它对我来说效果不佳
【发布时间】:2013-12-26 03:59:55
【问题描述】:

注意:我解决了我的问题。但是,它进行了一些增量更改。如果您遇到此页面,请随时查看我的 github,看看我是如何制作此应用程序的。


我正在使用 http-kit 向 btc-china 发布请求。我想使用他们的交易API。我可以用 python 很好地做到这一点,但由于某种原因,我一直使用 clojure 和 http-kit 得到 401。我在下面发布了一段代码,可能表明我没有正确使用 http-kit。除此之外,如果你想看的话,这里是我的完整代码的 github:https://github.com/gilmaso/btc-trading 以下是 btc-china api 文档: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))))

【问题讨论】:

  • 您是否尝试过比较两种情况下的实际 HTTP 交换?也许看到差异会给你一个提示。
  • 9000,我实际上不确定如何在 clojure 或 python 中做到这一点:/
  • wireshark这样的程序可以让你记录http流量,并在你方便的时候进行分析

标签: clojure http-kit


【解决方案1】:

引用example on the bttchina site:

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

clojure 哈希映射是无序的,如果顺序很重要,您不能使用 clojure 哈希映射文字来提供输入

【讨论】:

  • 我认为顺序只对哈希本身很重要,而对查询参数无关。话虽如此,看起来 clj-http 和 http-kit 都只允许其查询参数使用哈希图。
  • @user3081629 您可以使用 sorted-map/sorted-map-by(或仅排序图)进行哈希计算
  • edbond,我回去将地图更改为排序地图。我自己应该想到这一点,但我对clojure真的很陌生。话虽如此,401 仍然存在。
【解决方案2】:

我在使用 bitstamp api 时遇到了非常相似的问题。解决方案是将:query-params 替换为:form-params。然后参数在正文中发送。我注意到在您的 api 中,您是在正文中手动发送的。看起来使用 :form-params 可能对您的情况也有帮助。

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 2019-07-09
    • 2021-03-31
    • 2017-01-28
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多