我是Clojure的新手,我想知道我是否正确设置了这个卷曲调用。在文件的名称空间中,我定义了将clojure.java.shell作为变量sh运行的用法,然后使用命令curl和完整的url调用sh来执行GET卷曲请求。
ns my-exercise.search
(:require [hiccup.page :refer [html5]])
(:use [clojure.java.shell :only [sh]]))
(def response (sh "curl" completeURL))
(println response)
当我在终端中打印响应时,我得到一张表,看起来它有关于响应性能的数据。但是,输出是空的。
我是否正在调用curl命令,因为我在终端中收到响应?
{:exit 0, :out (), :err % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2 100 2 0 0 7 0 --:--:-- --:--:-- --:--:-- 7
}
如果是的话,那我还有另外一个问题。我收到一个空的parens作为输出响应。即使我使用Postman拨打电话,我也会收到状态200,数据是空的parens。这是否意味着api没有向我发送信息?
我的上一个问题,让我说我确实让它工作并获得输出的响应。如何获得该输出并将其分配给变量?
谢谢!
答案 0 :(得分:1)
我可以建议一个替代的,更惯用的解决方案 - 使用http库吗?我使用clj-http / cljs-http,它真的很震撼,因为它了解Clojure数据结构,并允许您从不同的序列化选项中进行选择。以下是我将如何在clj-http中生成相同的请求。
将clj-http添加到project.clj中的依赖项
[clj-http "3.7.0"]
以ns格式要求clj-http:
[clj-http.client :as http]
请求代码:
(http/get completeURL {:query-params {} ;; you can submit query params here
:as :json}) ;; decode response as json
;; other options are
;; :byte-array - to get response as byte array
;; "UTF-16" - to get response as UTF-16 string
;; :transit+json - to decode response using transit
;; or omit :as option to get response as default string
找到完整的文档
答案 1 :(得分:0)
我是这样做的:
* {
box-sizing: border-box;
}
#outercontainer {
display: flex;
flex-direction: row;
}
#leftcolumn {
flex: 0 1 75%;
display: flex;
flex-direction: column;
}
#leftrow {
display: flex;
flex-direction: row;
}
#daterange {
background-color: yellow;
flex: 1 0 25%;
text-align: center;
padding: 10px;
}
#zip {
background-color: lightgreen;
flex: 1 0 25%;
text-align: center;
padding: 10px;
}
#keywords {
background-color: lightblue;
flex: 1 0 25%;
text-align: center;
padding: 10px;
flex-grow: 1;
}
#operations {
background-color: red;
text-align: center;
padding: 10px;
}
#map {
flex: 1 0 25%;
background-color: pink;
text-align: center;
padding: 10px;
}
结果:
<div id="outercontainer">
<div id="map">map </div>
<div id="leftcolumn">
<div id="leftrow">
<div id="daterange">daterange </div>
<div id="zip">zip </div>
<div id="keywords">keywords </div>
</div>
<div id="operations">operations </div>
</div>
</div>
尝试(defn run-shell-cmd
"Run a command represented as a string in an OS shell (default=/bin/bash).
Example: 'ls -ldF *' "
[cmd-str]
(let [result (shell/sh *os-shell* "-c" cmd-str)]
(if (= 0 (t/safe-> :exit result))
result
(throw (RuntimeException.
(str "shell-cmd: clojure.java.shell/sh failed. \n"
"cmd-str:" cmd-str "\n"
"exit status:" (:exit result) "\n"
"stderr:" (:err result) "\n"
"result:" (:out result) "\n"
))))))
(let [result (run-shell-cmd "ls -al")]
(newline)
(println :ls-cmd)
(println result))
:
:ls-cmd
{:exit 0, :out total 176
drwxrwxr-x 8 alan alan 4096 Mar 14 13:46 .
drwxr-xr-x 155 alan alan 4096 Mar 28 17:27 ..
drwxrwxr-x 8 alan alan 4096 Mar 28 17:29 .git
结果:
curl
如果您不想自己动手,可以find the above convenience function here。