使用groovy执行curl

时间:2015-11-09 10:18:32

标签: groovy

我有代码Parser.groovy文件:

def json = """{
        "description": "string",
        "mode": "DEFAULT",
        "name": "string",
        "start_time": "2015-11-05T13:26:40.626Z",
        "tags": [
                "string"
        ]
    }"""
    process = ["curl", "-k", "--user", "user:pass", "-X", "POST", "-H", "Content-Type: application/json", "-d", "${json}", "https://<api_uri>/launch"].execute().text

当我执行它时,我得到了这个例外:

Caught: java.io.IOException: Cannot run program "curl": CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program "curl": CreateProcess error=2, The system cannot find the file specified
    at Parser.run(Parser.groovy:19)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    ... 2 more

我做错了什么?我是groovy的新人,请解释我的错误。

2 个答案:

答案 0 :(得分:2)

你应该可以用普通的groovy和wslite库来做到这一点:

@Grab('com.github.groovy-wslite:groovy-wslite:1.1.2')
import wslite.http.auth.*
import wslite.rest.*

def client = new RESTClient("https://<api_uri>/")
client.authorization = new HTTPBasicAuthorization("user", "pass")
def response = client.post(path: "/launch",headers: ['Content-Type': 'application/json']) {
    json description: "string", mode: "DEFAULT", name: "string", start_time: "2015-11-05T13:26:40.626Z", tags: [ "string" ] 
}

答案 1 :(得分:1)

您的程序找不到curl.exe。指定完整路径......

process = ["c:\\whatever\\curl", "-k", "--user" ...

...或在调用程序之前相应地设置环境变量PATH

set path=C:\whatever;%path%

除此之外,使用库进行http / url通信可能更稳定。我不会推荐一个,但我相信你会在这里找到很多例子。