Perfectly working curl command fails when executed in a groovy script

这一生的挚爱 提交于 2019-12-03 01:42:52
Will

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!