Search code examples
curlgroovyasana

Curl command in a Groovy-script fails to execute (in Linux)


I try to implement a curl command in a groovy script. The goal off my command is to create a task in Asana.

Is it possible that groovy changes the command at runtime? (is it possible that groovy has some problems with the data-urlencode parameter) Has this something to do with the version of curl? (version 7.64.0) (works perfectly when I run it seperatly)

The command works perfectly in a command prompt but fails when I try to run it in a groovy-script.

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("[email protected]")){
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 abc!")
31 }

This is the command that I try to run: (I also tried the -v option but that doesn't produced useful output)

curl -H "Authorization: Bearer " https://app.asana.com/api/1.0/tasks --data-urlencode "projects=projectId" --data-urlencode "notes=message" --data-urlencode "name=1008"

And this produces the following output curl: option -: is unknown curl: try 'curl --help' or 'curl --manual' for more information

Can anyone help me with my groovy-script? Thanks in advance!


Solution

  • Try to prepend the command with the shell, i.e. def command="/bin/bash curl https..."