Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this curl command fail when executed in groovy?

Tags:

curl

groovy

This curl command works in the terminal but fails in groovy. I added the err and out out from another question to try and understand why it fails.

def initialSize = 4096
def out = new ByteArrayOutputStream(initialSize)
def err = new ByteArrayOutputStream(initialSize)
def process = "sh -c curl'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts'".execute()
process.consumeProcessOutput(out, err)
process.waitFor()
println process.text
println err.toString()
println out.toString()

The output is "curl: try 'curl --help' or 'curl --manual' for more information"

like image 413
Brian Avatar asked May 20 '26 17:05

Brian


1 Answers

Don't use string for execute, as Groovy will split on whitespace. You have to pass the "shell command" as a single argument to sh -c. So right now you a) lack a space between curl and the url and b) this will end up as two arguments (and you can not quote for that).

Use a string list instead:

['sh', '-c', "curl 'http://...'"].execute()

Also on a sidenote: if you just want the content of a url and don't need fancy things (timeouts, auth, ...), you can just as well do:

"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts".toURL().text
like image 130
cfrick Avatar answered May 23 '26 01:05

cfrick