Perfectly working curl command fails when executed

2019-03-18 02:08发布

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.

4条回答
在下西门庆
2楼-- · 2019-03-18 02:30

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
查看更多
Luminary・发光体
3楼-- · 2019-03-18 02:36

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

查看更多
男人必须洒脱
4楼-- · 2019-03-18 02:44

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()
查看更多
forever°为你锁心
5楼-- · 2019-03-18 02:45

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().text

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

查看更多
登录 后发表回答