【发布时间】:2019-08-04 10:41:06
【问题描述】:
我尝试在 groovy 脚本中实现 curl 命令。 我的目标是在 Asana 中创建一个任务。
groovy 是否有可能在运行时更改命令? (是不是groovy的data-urlencode参数有问题) 这与 curl 的版本有关吗? (版本 7.64.0) (当我单独运行它时完美运行)
该命令在命令提示符下完美运行,但当我尝试在 groovy 脚本中运行它时失败。
1 def opsAlert = opsgenie.getAlert(alertId: "${alert.alertId}");
2 //logger.warn("${opsAlert}"); //DEBUG
3 if(opsAlert.isEmpty()){
4 throw new Exception("Failed to get the alert details, opsAlert is null!");
5 }
6
7 if(opsAlert.source.equals("bclaeys@oxya.com")){
8 def projects = "<projectid>";
9 def name = opsAlert.details.sensorId;
10 def notes = opsAlert.message;
11 def authorizationToken = "Bearer <mytoken>";
12
13 def command = "curl https://app.asana.com/api/1.0/tasks ";
14 command += "-H \"Authorization: ${authorizationToken}\" ";
15 command += "--data-urlencode \"projects=${projects}\" ";
16 command += "--data-urlencode \"notes=${notes}\" ";
17 command += "--data-urlencode \"name=${name}\"";
18 logger.warn("${command}"); //DEBUG
19
20 def commandResult = "${command}".execute();
21 commandResult.waitFor();
22 logger.warn("${commandResult}");
23
24 //def output1 = commandResult.text;
25 def output2 = commandResult.err.text;
26 //logger.warn("${output1}"); //DEBUG
27 logger.warn("${output2}"); //DEBUG
28 return;
29 } else {
30 throw new Exception("S(s)ource was not bclaeys!")
31 }
这是我尝试运行的命令:(我也尝试了 -v 选项,但没有产生有用的输出)
curl -H "Authorization: Bearer" https://app.asana.com/api/1.0/tasks --data-urlencode "projects=projectId" --data-urlencode "notes=message" --data-urlencode "name=1008"
这会产生以下输出 curl:选项-:未知 curl:尝试“curl --help”或“curl --manual”以获取更多信息
谁能帮助我的 groovy 脚本?提前致谢!
【问题讨论】: