Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfectly working curl command fails when executed in a groovy script

I have a post commit hook (a groovy script) in gitblit to invoke a REST endpoint. In this script I am executing a curl command. But it seems to fail. The curl command works fine when executed from the commandline.

Following is my groovy script.

#!/usr/bin/env groovy


def repoUrl= "https://gitblit.myhost.com/git/" + repository + ".git"
json='{"repository":{"url":"'+repoUrl+'"}}'

def response = "curl -v -k -X POST -H \"Content-Type: application/json\" -d '${json}' https://username:[email protected]:9443/restendpoint".execute().text
println response 

repository is passed by gitblit to this script and I have verified it.

Can somebody help me with this.

like image 779
Amila Maharachchi Avatar asked May 19 '14 16:05

Amila Maharachchi


People also ask

How do I run curl command in Groovy script?

You could execute the curl command as a shell command using the "execute()" method available on groovy strings. Or you can use some native html processing classes. This will give you some native parsing of the response and response error handling etc.

Why my curl command is not working?

We might have come across errors like “curl: command not found” while working in the terminal. This type of error comes due to only one reason: the relevant package is not installed. Curl is a very popular data transfer command-line utility used for downloading and uploading data from or to the server.

How do I know if my curl command is working?

To check whether the Curl package is installed on your system, open up your console, type curl , and press enter. If you have curl installed, the system will print curl: try 'curl --help' or 'curl --manual' for more information .

How do I use the curl command in Jenkins pipeline?

The way to execute curl command is to use sh (or bat if you are on the Windows server) step. You need to know that the sh step by default does not return any value, so if you try to assign it's output to a variable, you will get the null value.


2 Answers

I couldn't reproduce your problem with your example, but i will try a wild guess:

First, use the list execute() version, so you don't have problems with tokens:

process = [ 'bash', '-c', "curl -v -k -X POST -H \"Content-Type: application/json\" -d '${json}' https://username:[email protected]:9443/restendpoint" ].execute()

Second, read both error and output from the process:

process.waitFor()
println process.err.text
println process.text

The err may give out what is going on

like image 55
Will Avatar answered Oct 19 '22 09:10

Will


I was able to get this working by passing all the string in my curl command in an array. Following is how I did it.

def response = ["curl", "-k", "-X", "POST", "-H", "Content-Type: application/json", "-d", "${json}", "https://username:[email protected]:9443/restendpoint"].execute().text
like image 23
Amila Maharachchi Avatar answered Oct 19 '22 07:10

Amila Maharachchi