29

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:password@anotherhost.com: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.

Amila Maharachchi
  • 2,121
  • 2
  • 15
  • 21

4 Answers4

30

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:password@anotherhost.com: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

RNK
  • 5,582
  • 11
  • 65
  • 133
Will
  • 14,348
  • 1
  • 42
  • 44
  • 1
    Thanks. I was searching for hours, but your solution worked for me. Why does the list version have to be used instead of a regular string? – The Unknown Dev Feb 09 '15 at 13:56
  • @Jamil, i guess it's because the runtime execution can do the correct separation of arguments/commands and escaping, otherwise it might get ambiguous. – Will Feb 09 '15 at 14:00
  • If anyone is curious, there is a brief explanation on separating arguments here: http://groovy.codehaus.org/Executing+External+Processes+From+Groovy. – The Unknown Dev Feb 17 '15 at 14:46
  • 3
    I think the `.text` is wrong in the first code block, isn't it? – crusy Apr 10 '19 at 06:38
  • How do we retrieve HTTP code from groovy execute method ? – nilesh1212 Jun 25 '20 at 12:57
23

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:password@myhost.com:9443/restendpoint"].execute().text
Amila Maharachchi
  • 2,121
  • 2
  • 15
  • 21
0

To avoid ‘running-forever’ process (this happens on some Windows env when output exceeds 4096 bytes) add initial size to ByteArrayOutputStream

def initialSize = 4096
def out = new ByteArrayOutputStream(initialSize)
def err = new ByteArrayOutputStream(initialSize)
def proc = command.execute()
proc.consumeProcessOutput(out, err)
proc.waitFor()
bartoleo
  • 299
  • 1
  • 7
  • 1
    Having a hard time seeing how this is really answering the question asked here... – Andrew Barber May 20 '14 at 14:26
  • @AndrewBarber not doing this can cause hanging processes. The asker didn't specify how the command failed so it's a fair guess, and we're stuck guessing anyway. – tsiki Aug 27 '15 at 15:33
  • I'm actually googling to solve a "running forever" process process issue (non windows) and since this was the first post I found relating to it, I wanted to say thanks for posting. Even though its not really related to OP's question, I think its sending me down the path toward fixing my issue, so thanks ! – David Farrell Jul 22 '22 at 22:28
0

In Curl Post -- In-F option - wrap the entire param with double quotes .Don't forget to escape the double quotes to get syntax right. Example Below:

def response = "curl -u admin:admin -F\"jcr:content/par/address/address1=2/3 Market Place\" http://localhost:4502/content/datasource/branches".execute().text

Dkumar
  • 1
  • 1