首页 > 解决方案 > 使用 clojure 从 api 获取数据并记录结果

问题描述

我对 clojure 很陌生,我熟悉 java 和 PHP。我已经分配了新的 clojure 作业来从外部 API 获取数据并执行一些过程。

这是我的 API 请求,我使用 curl 命令进行了尝试 curl -i -X POST -H "Content-Type: application/json" -d "{\"uid\":\"uId\", \"param\":\"1\"}" http://localhost:8081/api/getData

结果是 - {"comp":true,"yUi":"78.2555","xUi":"4.5233","id":"10"}

我已经使用 clojure 应用了上述过程,我需要记录这些返回值。你能帮我用 clojure 来做吗

这就是我尝试的方式

(defn- get-data
  "api call"
  [uId]
  (try
    (log/info (trs "start process: {0}" (str "uid" uId )))
    (when-let [ui (client/post "http://localhost:8081/api/getData" {:form-params {:uid uId,:param "1"} :content-type :json })]
      (log/info (trs "get data: {0} {1} {2} {3}" (:comp ui) (:yUi ui) (:xUi ui) (:id ui))))
    (catch Throwable e
      (log/error "api call failed:" (.getMessage e)))))

标签: clojure

解决方案


推荐阅读